rosetta icon indicating copy to clipboard operation
rosetta copied to clipboard

Some usage questions

Open Nutomic opened this issue 2 years ago • 4 comments

Thank you for this library! There are very few options for using json localization files in Rust, and this looks like the best one. There are some things which are unclear though, and I hope you can explain them to me:

  • How can i select the language dynamically? The examples only show Lang::En.hello(), but i need something like get_string("en", "hello") in order to select the language at runtime.
  • Can errors about unused arguments be disabled? We are using the same translation files across multiple projects, so its normal that some of them are unused.
    error: named argument never used
    --> /home/felix/workspace/lemmy/target/debug/build/lemmy_websocket-5c9ec7d2402ea26c/out/rosetta_output.rs:1:1957
    |
    1 | ...tring { match self { _ => format ! ("{{count}} Abonnés" , count = count) } } # [allow (clippy :: match_single_binding)] pub fn select_...
    |                                        -------------------           ^^^^^ named argument never used
    |                                        |
    |                                        formatting specifier missing
    
  • When enabling translations for en and fr from these files, it gives another error. Maybe its because we dont follow your string formatting rules, or because the variable has a different name in both language files?
    Compiling lemmy_websocket v0.15.1 (/home/felix/workspace/lemmy/crates/websocket)
    error: failed to run custom build command for `lemmy_websocket v0.15.1 (/home/felix/workspace/lemmy/crates/websocket)`
    
    Caused by:
    process didn't exit successfully: `/home/felix/workspace/lemmy/target/debug/build/lemmy_websocket-d9289556ed2bb147/build-script-build` (exit status: 1)
    --- stdout
    cargo:rerun-if-changed=translations/translations/en.json
    
    --- stderr
    Error: Parse(InvalidType { key: "number_online_plural", expected: "string" })
    

Thanks in advance :)

Nutomic avatar Jan 20 '22 22:01 Nutomic

Hello! Thanks for your interest in this project :smile:

How can i select the language dynamically?

The generated Lang type implement the Language trait and have a from_language_id() method to get the variant corresponding to a specific language. The LanguageId type actually only support ISO_639-1 format, it is planned to switch to IETF BCP 47 language tags that are used by HTML and HTTP (see #3).

Can errors about unused arguments be disabled?

The translation strings are actually directly used in the format! macro, so only variables enclosed with single braces are supported ({count} instead of {{count}}). It looks like parsing doesn't fail when you have variables with two braces, but it is not properly recognized by the format! macro.

When enabling translations for en and fr from these files, it gives another error.

The variables do need to have the same name, otherwise the parsing fails. This allows to have multiple variables that are not in the same order in the different translations.

I plan to rewrite the entire parser and code generator in a next release to support more syntaxes (like variables with two braces or nested keys) and maybe consider other file formats than json (like Fluent).

baptiste0928 avatar Jan 22 '22 10:01 baptiste0928

The generated Lang type implement the Language trait and have a from_language_id() method to get the variant corresponding to a specific language. The LanguageId type actually only support ISO_639-1 format, it is planned to switch to IETF BCP 47 language tags that are used by HTML and HTTP (see #3).

Thats it. I would suggest you add an example in the readme/documentation, because its not mentioned anywhere, and wasnt clear to me at all.

The variables do need to have the same name, otherwise the parsing fails. This allows to have multiple variables that are not in the same order in the different translations.

Okay i will see how to fix our translation files then.

I plan to rewrite the entire parser and code generator in a next release to support more syntaxes (like variables with two braces or nested keys) and maybe consider other file formats than json (like Fluent).

Any rough estimate how long this rewrite would take? Like if its a matter of weeks, or months, to determine whether i should wait for the new version, or change our files for now.

Nutomic avatar Jan 22 '22 12:01 Nutomic

Any rough estimate how long this rewrite would take?

I haven't much free time right now, I think I'll start working on it in the next month or two.

baptiste0928 avatar Jan 23 '22 10:01 baptiste0928

Great, thank you :)

Nutomic avatar Jan 26 '22 16:01 Nutomic