Skip to content

Commit 108b86b

Browse files
committed
feat(i18n): add t_args method for localized strings with arguments
1 parent 738740c commit 108b86b

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/i18n.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Internationalization support using Fluent.
22
33
use bevy::prelude::*;
4+
pub use fluent_bundle::FluentArgs;
45
use fluent_bundle::FluentResource;
56
use fluent_bundle::concurrent::FluentBundle;
67
use std::sync::Arc;
@@ -110,6 +111,21 @@ impl I18n {
110111
}
111112
}
112113

114+
/// Get a localized string with named arguments.
115+
pub fn t_args(&self, id: &str, args: &fluent_bundle::FluentArgs) -> String {
116+
let msg = self.bundle.get_message(id);
117+
match msg {
118+
Some(msg) => {
119+
let pattern = msg.value().expect("message has no value");
120+
let mut errors = vec![];
121+
self.bundle
122+
.format_pattern(pattern, Some(args), &mut errors)
123+
.to_string()
124+
}
125+
None => id.to_string(),
126+
}
127+
}
128+
113129
fn build_bundle(locale: Locale, custom: &[(Locale, String)]) -> FluentBundle<FluentResource> {
114130
let lang_id = locale.lang_id();
115131
let source = locale.ftl_source();

0 commit comments

Comments
 (0)