-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Localization of Widgets strings #426
Changes from all commits
b0cd2aa
a63afaa
fd610f9
f27ea4a
866aadf
d2b89b3
575029f
f694933
a462e9e
01ce3cf
c39308f
d4593eb
89305f5
dfc2a94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//! Localization of texts in widgets and containers. | ||
use std::default::Default; | ||
|
||
/// Handles the localization of default texts in [`crate::widgets`] and [`crate::containers`]. | ||
/// | ||
/// You can set the current language with [`crate::Context::set_localization`]. For example: `ctx.set_localization(Language::BahasaMalaysia)`. | ||
#[derive(Clone, Debug)] | ||
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] | ||
#[cfg_attr(feature = "persistence", serde(default))] | ||
pub struct Localization { | ||
/// The current language used for texts. | ||
pub lang: Language, | ||
|
||
// Texts for sliders | ||
pub slider_tooltip: &'static str, | ||
|
||
// Texts for colour pickers | ||
pub click_copy: &'static str, | ||
pub cp_edit: &'static str, | ||
pub cp_blending: &'static str, | ||
pub cp_additive: &'static str, | ||
pub cp_normal: &'static str, | ||
pub cp_selected_color: &'static str, | ||
pub cp_hue: &'static str, | ||
pub cp_saturation: &'static str, | ||
pub cp_value: &'static str, | ||
pub lang_text: &'static str, | ||
} | ||
|
||
impl Default for Localization { | ||
/// Sets English as the default language for texts. | ||
/// | ||
/// It can also be used to switch from another language to English. | ||
fn default() -> Self { | ||
Self { | ||
lang: Language::English, | ||
slider_tooltip: "Drag to edit or click to enter a value.\nPress 'Shift' while dragging for better control", | ||
click_copy: "Click to copy", | ||
cp_edit: "Click to edit color", | ||
cp_blending: "Blending", | ||
cp_additive: "Additive", | ||
cp_normal: "Normal", | ||
cp_selected_color: "Selected color", | ||
cp_hue: "Hue", | ||
cp_saturation: "Saturation", | ||
cp_value: "Value", | ||
lang_text: "Language", | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Clone)] | ||
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] | ||
/// Specifies the languages currently available for localization and is required by [`crate::Context::set_localization`] as the parameter type. | ||
pub enum Language { | ||
English, | ||
BahasaMalaysia, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this list? Can't the user just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Agree, some clarification: |
||
} | ||
|
||
impl Default for Language { | ||
fn default() -> Self { | ||
Language::English | ||
} | ||
} | ||
|
||
impl Localization { | ||
/// Pattern matches on ['Language'] to call the function that'll set the fields within Localization accordingly. | ||
pub fn set_localization(&mut self, lang: Language) { | ||
*self = match lang { | ||
Language::English => Localization::default(), | ||
Language::BahasaMalaysia => Localization::malay(), | ||
}; | ||
} | ||
|
||
/// Returns the current language used for texts. | ||
pub fn lang(&self) -> Language { | ||
match self.lang { | ||
Language::BahasaMalaysia => Language::BahasaMalaysia, | ||
_ => Language::English, | ||
} | ||
} | ||
|
||
/// Sets Bahasa Malaysia as the language for texts. | ||
fn malay() -> Self { | ||
Self { | ||
lang: Language::BahasaMalaysia, | ||
slider_tooltip: "Tarik untuk ubah atau klik untuk masukkan jumlah.\nTekan 'Shift' sambil tarik untuk pergerakan lebih terkawal.", | ||
click_copy: "Klik untuk salin", | ||
cp_edit: "Klik untuk ubah warna", | ||
cp_blending: "Campuran", | ||
cp_additive: "Tambahan", | ||
cp_normal: "Biasa", | ||
cp_selected_color: "Warna pilihan", | ||
cp_hue: "Rona", | ||
cp_saturation: "Ketepuan", | ||
cp_value: "Nilai", | ||
lang_text: "Bahasa", | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
use crate::{any, area, window, Id, InputState, LayerId, Pos2, Rect, Style}; | ||
use crate::{ | ||
any, area, localization::Language, window, Id, InputState, LayerId, Pos2, Rect, Style, | ||
}; | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
|
@@ -13,6 +15,7 @@ use crate::{any, area, window, Id, InputState, LayerId, Pos2, Rect, Style}; | |
/// | ||
/// If you want to store data for your widgets, you should look at `data`/`data_temp` and | ||
/// `id_data`/`id_data_temp` fields, and read the documentation of [`any`] module. | ||
|
||
#[derive(Clone, Debug, Default)] | ||
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] | ||
#[cfg_attr(feature = "persistence", serde(default))] | ||
|
@@ -53,6 +56,9 @@ pub struct Memory { | |
/// new fonts that will be applied at the start of the next frame | ||
pub(crate) new_font_definitions: Option<epaint::text::FontDefinitions>, | ||
|
||
/// new language that will be applied at the start of the next frame | ||
pub(crate) new_language: Option<Language>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for this - let's make it instant instead. |
||
|
||
#[cfg_attr(feature = "persistence", serde(skip))] | ||
pub(crate) interaction: Interaction, | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.