File tree Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ static mut JSON_DATA_GUARD: Option<Mutex<()>> = None;
14
14
static DICTIONARY : ArcSwapOption < Dictionary > = ArcSwapOption :: const_empty ( ) ;
15
15
16
16
pub type StoreFn = dyn Send + Sync + Fn ( & str ) -> Result < ( ) > + ' static ;
17
+ pub type DictionaryArgs < ' a > = Vec < ( & ' static str , & ' a str ) > ;
17
18
18
19
pub struct Builder {
19
20
current_code : String ,
@@ -272,6 +273,21 @@ pub fn i18n(text: &str) -> &str {
272
273
}
273
274
}
274
275
276
+ /// Translate a string to the currently user-selected language
277
+ /// and replace given placeholders with given values.
278
+ /// Parameter 'replacements' is a vector consisting of key value pairs,
279
+ /// where the key is the placeholder within 'text'.
280
+ pub fn i18n_args ( text : & str , replacements : DictionaryArgs ) -> String {
281
+ let mut translated = String :: from ( i18n ( text) ) ;
282
+
283
+ for ( key, value) in replacements {
284
+ let placeholder = format ! ( "{{{}}}" , key) ;
285
+ translated = str:: replace ( & translated, & placeholder, value) ;
286
+ }
287
+
288
+ translated
289
+ }
290
+
275
291
/// Dictionary structure containing all translations and related data.
276
292
pub struct Dictionary {
277
293
/// list of languages {"en": "English", "ja": "日本語", ..."}
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ pub mod json;
14
14
pub mod result;
15
15
16
16
pub use i18n:: i18n;
17
+ pub use i18n:: i18n_args;
17
18
18
19
pub mod prelude {
19
20
pub use crate :: i18n;
You can’t perform that action at this time.
0 commit comments