Improve i18n "get" performances by returning a &str

This commit is contained in:
Rémy PHELIPOT
2020-01-21 21:05:02 +01:00
parent 8aa1970fdf
commit f60a68d96d
6 changed files with 14 additions and 12 deletions

View File

@ -48,10 +48,10 @@ impl VoxygenLocalization {
/// ///
/// If the key is not present in the localization object /// If the key is not present in the localization object
/// then the key is returned. /// then the key is returned.
pub fn get(&self, key: &str) -> String { pub fn get<'a>(&'a self, key: &'a str) -> &'a str {
match self.string_map.get(key) { match self.string_map.get(key) {
Some(localized_text) => localized_text.to_owned(), Some(localized_text) => localized_text,
None => key.to_string(), None => key,
} }
} }

View File

@ -128,7 +128,8 @@ impl PlayState for CharSelectionState {
clock.get_last_delta(), clock.get_last_delta(),
|_| {}, |_| {},
) { ) {
global_state.info_message = Some(localized_strings.get("common.connection_lost")); global_state.info_message =
Some(localized_strings.get("common.connection_lost").to_owned());
error!("[session] Failed to tick the scene: {:?}", err); error!("[session] Failed to tick the scene: {:?}", err);
return PlayStateResult::Pop; return PlayStateResult::Pop;

View File

@ -1135,13 +1135,13 @@ impl CharSelectionUi {
self.imgs.slider_range, self.imgs.slider_range,
); );
let char_slider = move |prev_id, let char_slider = move |prev_id,
text: String, text: &str,
text_id, text_id,
max, max,
selected_val, selected_val,
slider_id, slider_id,
ui_widgets: &mut UiCell| { ui_widgets: &mut UiCell| {
Text::new(&text) Text::new(text)
.down_from(prev_id, 22.0) .down_from(prev_id, 22.0)
.align_middle_x_of(prev_id) .align_middle_x_of(prev_id)
.font_size(18) .font_size(18)

View File

@ -156,7 +156,7 @@ impl PlayState for MainMenuState {
if let Some(info) = global_state.info_message.take() { if let Some(info) = global_state.info_message.take() {
self.main_menu_ui self.main_menu_ui
.show_info(info, localized_strings.get("common.okay")); .show_info(info, localized_strings.get("common.okay").to_owned());
} }
// Draw the UI to the screen. // Draw the UI to the screen.

View File

@ -385,8 +385,8 @@ impl MainMenuUi {
self.connect = true; self.connect = true;
self.connecting = Some(std::time::Instant::now()); self.connecting = Some(std::time::Instant::now());
self.popup = Some(PopupData { self.popup = Some(PopupData {
msg: localized_strings.get("main.connecting") + "...", msg: [localized_strings.get("main.connecting"), "..."].concat(),
button_text: localized_strings.get("common.cancel"), button_text: localized_strings.get("common.cancel").to_owned(),
popup_type: PopupType::ConnectionInfo, popup_type: PopupType::ConnectionInfo,
}); });
@ -423,8 +423,8 @@ impl MainMenuUi {
self.connect = true; self.connect = true;
self.connecting = Some(std::time::Instant::now()); self.connecting = Some(std::time::Instant::now());
self.popup = Some(PopupData { self.popup = Some(PopupData {
msg: localized_strings.get("main.creating_world") + "...", msg: [localized_strings.get("main.creating_world"), "..."].concat(),
button_text: localized_strings.get("common.cancel"), button_text: localized_strings.get("common.cancel").to_owned(),
popup_type: PopupType::ConnectionInfo, popup_type: PopupType::ConnectionInfo,
}); });
}; };

View File

@ -374,7 +374,8 @@ impl PlayState for SessionState {
// Perform an in-game tick. // Perform an in-game tick.
if let Err(err) = self.tick(clock.get_avg_delta()) { if let Err(err) = self.tick(clock.get_avg_delta()) {
global_state.info_message = Some(localized_strings.get("common.connection_lost")); global_state.info_message =
Some(localized_strings.get("common.connection_lost").to_owned());
error!("[session] Failed to tick the scene: {:?}", err); error!("[session] Failed to tick the scene: {:?}", err);
return PlayStateResult::Pop; return PlayStateResult::Pop;