Skip to content
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

Some usage questions #5

Open
Nutomic opened this issue Jan 20, 2022 · 4 comments
Open

Some usage questions #5

Nutomic opened this issue Jan 20, 2022 · 4 comments

Comments

@Nutomic
Copy link

Nutomic commented Jan 20, 2022

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 :)

@baptiste0928
Copy link
Owner

Hello! Thanks for your interest in this project 😄

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).

@Nutomic
Copy link
Author

Nutomic commented Jan 22, 2022

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.

@baptiste0928
Copy link
Owner

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.

@Nutomic
Copy link
Author

Nutomic commented Jan 26, 2022

Great, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants