-
-
Notifications
You must be signed in to change notification settings - Fork 218
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
I18n #434
base: main
Are you sure you want to change the base?
I18n #434
Conversation
Looks like a good start! I wonder if we can simplify a bit, though. Can we make a template more like this? #[derive(Template)]
#[template(path = "i18n.html")]
struct UsesI18n<'a> {
#[locale]
loc: unic_langid::LanguageIdentifier,
name: &'a str,
hours: f64,
} Where the type of the field marked as locale must implement a (Since a language identifier will seemingly always just be parsed from a |
Also, thanks for working on this! I'm very excited to get this in! |
How would we know to use Fluent does not allow positional arguments. Allowing a |
I guess my question is, what bits of state (other than the locale) does a |
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.
Basically: We need to initialize a static loader (with fallback language) and somehow have access to that loader in different parts of the code.
} | ||
$v struct $n { | ||
language: unic_langid::LanguageIdentifier, | ||
loader: &'static fluent_templates::once_cell::sync::Lazy<fluent_templates::StaticLoader> |
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.
We need to be able to access this loader somehow. That's the reason for this struct.
// The directory of localisations and fluent resources. | ||
locales: $locales, | ||
// The language to falback on if something is not present. | ||
fallback_language: $fallback_language, |
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.
The fallback language can be added to the toml file.
][..]; | ||
|
||
// create default localizer | ||
let localizer = super::#loc_ty::default(); |
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.
Here we need to be able to somehow access the static loader. Currently an object of the generated struct type is used for that.
self.localized_messages.insert(message.clone()); | ||
|
||
buf.write(&format!( | ||
"self.{}.translate(\"{}\", &std::iter::FromIterator::from_iter(vec![", |
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.
Here we need to be able to call the translate method. Currently it uses the Localizer object that has to be added to all Template structs.
Huh, sorry I missed your responses on this -- are you still interested in working on this? Having reviewed your code a bit more, how about having an Askama-provided type |
@djc I would certainly be interested in working on this. I'm not completely sure if I understand your solution with the Askama-provided |
Something like this: #[derive(Template)]
#[template(source = "{{ localize(\"hello\", age: 37) }}")]
struct UsesI18n<'a> {
#[locale]
loc: askama::Locale::new(lang_id, &TEMPLATES),
} with this struct Locale {
...
}
impl Locale {
fn new(lang: unic_langid::LanguageIdentifier, templates: &fluent_templates::StaticLoader) { ... }
} |
This (draft) PR gives an initial onset for localization (#202) by using fluent-templates to statically load all fluent translations.
It is based on code of @kazimuth
@djc Could you give some quick remarks / questions?
TODO
with-i18n
featurestext_id
's (the test fails, as it should, but we should assert that it fails instead of failing)init_translations
macro