1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
extern crate proc_macro;
use crate::proc_macro::TokenStream;
use i18n_config::I18nConfig;
use quote::quote;
use std::path::PathBuf;
use syn;
#[proc_macro_derive(I18nEmbed)]
pub fn i18n_embed_derive(input: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(input).unwrap();
let struct_name = &ast.ident;
let gen = quote! {
impl I18nEmbed for #struct_name {
}
};
gen.into()
}
#[proc_macro]
#[cfg(feature = "gettext-system")]
pub fn language_loader(input: TokenStream) -> TokenStream {
let struct_name = syn::parse_macro_input!(input as syn::Ident);
let config_file_path = PathBuf::from("i18n.toml");
if !config_file_path.exists() {
panic!(format!(
"The i18n configuration file '{}' does not exist in the current working directory '{}'",
config_file_path.to_string_lossy(),
std::env::current_dir().unwrap().to_str().unwrap()
));
}
let config = I18nConfig::from_file(&config_file_path).expect(&format!(
"language_loader!() had a problem reading config file '{0}'",
config_file_path.to_string_lossy()
));
let src_locale = &config.src_locale;
let gen = quote! {
struct #struct_name {
current_language: std::sync::RwLock<i18n_embed::unic_langid::LanguageIdentifier>,
}
impl #struct_name {
pub fn new() -> #struct_name {
#struct_name {
current_language: std::sync::RwLock::new(#src_locale.parse().unwrap()),
}
}
}
impl i18n_embed::LanguageLoader for #struct_name {
fn load_language_file(&self, language_id: i18n_embed::unic_langid::LanguageIdentifier, file: std::borrow::Cow<[u8]>) {
let catalog = i18n_embed::gettext::Catalog::parse(&*file).expect("could not parse the catalog");
i18n_embed::tr::set_translator!(catalog);
*(self.current_language.write().unwrap()) = language_id;
}
fn load_src_locale(&self) {
let catalog = i18n_embed::gettext::Catalog::empty();
i18n_embed::tr::set_translator!(catalog);
*(self.current_language.write().unwrap()) = self.src_locale();
}
fn domain(&self) -> &'static str {
i18n_embed::domain_from_module(module_path!())
}
fn src_locale(&self) -> i18n_embed::unic_langid::LanguageIdentifier {
#src_locale.parse().unwrap()
}
fn language_file_name(&self) -> String {
format!("{}.{}", self.domain(), "mo")
}
fn current_language(&self) -> i18n_embed::unic_langid::LanguageIdentifier {
self.current_language.read().unwrap().clone()
}
}
};
gen.into()
}