Skip to content

Commit

Permalink
use macro to generate language list
Browse files Browse the repository at this point in the history
  • Loading branch information
fralonra committed Dec 16, 2023
1 parent 9089459 commit 6409976
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 50 deletions.
75 changes: 25 additions & 50 deletions src/i18n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ use i18n_embed::{
use lazy_static::lazy_static;
use rust_embed::RustEmbed;

macro_rules! lang {
($id:tt, $label:tt) => {
Language {
id: $id,
label: $label,
}
};
}

#[derive(RustEmbed)]
#[folder = "i18n/"]
struct Localizations;
Expand All @@ -26,56 +35,22 @@ pub struct Language {
pub label: &'static str,
}

pub const LANGUAGES: [Language; 12] = [
Language {
id: "ar",
label: "العربية (Arabic)",
},
Language {
id: "de",
label: "Deutsch (German)",
},
Language {
id: "en",
label: "English (English)",
},
Language {
id: "es",
label: "Español (Spanish)",
},
Language {
id: "fr",
label: "Français (French)",
},
Language {
id: "it",
label: "Italiano (Italian)",
},
Language {
id: "ja",
label: "日本語 (Japanese)",
},
Language {
id: "ko",
label: "한국어 (Korean)",
},
Language {
id: "pt",
label: "Português (Portuguese)",
},
Language {
id: "ru",
label: "Русский (Russian)",
},
Language {
id: "zh_CN",
label: "简体中文 (Simplified Chinese)",
},
Language {
id: "zh_TW",
label: "繁體中文 (Traditional Chinese)",
},
];
const_array!(
pub LANGUAGES: Language [
lang!("ar", "العربية (Arabic)"),
lang!("de", "Deutsch (German)"),
lang!("en", "English (English)"),
lang!("es", "Español (Spanish)"),
lang!("fr", "Français (French)"),
lang!("it", "Italiano (Italian)"),
lang!("ja", "日本語 (Japanese)"),
lang!("ko", "한국어 (Korean)"),
lang!("pt", "Português (Portuguese)"),
lang!("ru", "Русский (Russian)"),
lang!("zh_CN", "简体中文 (Simplified Chinese)"),
lang!("zh_TW", "繁體中文 (Traditional Chinese)")
]
);

pub fn select_locales(request_languages: &[&'static str]) -> Result<()> {
let requested_languages: Vec<LanguageIdentifier> = request_languages
Expand Down
24 changes: 24 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
macro_rules! const_array {
(
$vis:vis $name:ident: $ty:ty [
$($item:expr),*
]
) => {
$vis const $name: [$ty; count_tts!($($item)*)] = [
$(
$item,
)*
];
};
}

macro_rules! count_tts {
($($tts:tt)*) => {0usize $(+ replace_expr!($tts 1usize))*};
}

macro_rules! fl {
($message_id:literal) => {{
i18n_embed_fl::fl!($crate::i18n::LANGUAGE_LOADER, $message_id)
Expand All @@ -7,3 +25,9 @@ macro_rules! fl {
i18n_embed_fl::fl!($crate::i18n::LANGUAGE_LOADER, $message_id, $($args), *)
}};
}

macro_rules! replace_expr {
($_t:tt $sub:expr) => {
$sub
};
}

0 comments on commit 6409976

Please sign in to comment.