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

Compile time checks #63

Open
woojiq opened this issue Sep 18, 2023 · 2 comments
Open

Compile time checks #63

woojiq opened this issue Sep 18, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@woojiq
Copy link

woojiq commented Sep 18, 2023

I didn't find any information about compile-time checks. If message in locale file is missing then string passed to macro will be displayed: https://github.com/longbridgeapp/rust-i18n/blob/ba2c7cb8e1083ccc12c8887623a43c25bf9ee4d6/crates/macro/src/lib.rs#L183-L186

Will it be hard to create a separate macro or add option for t!() to make sure the message I use exists in the locale file at compile time (like sqlx query! does)? This can be tricky, so maybe an easier way is to create xtask (or test) to check that all the messages I'm using in the code are defined. This way it can be easily integrated with CI.
All I want to achieve is if my app has passed CI, the locale files will have the appropriate content and the user won't see "en.wron_mesag".

Probably internationalization-rs can be used to see how they achieve compile time verification (using build.rs).

@SteveLauC
Copy link

Can we implement this by parsing all the Rust source code, and for the macro invocation, check if its ident is t, if so, extract the translation key, then we can do the needed checks. 🤔

This should work for code like:

t!("translation key");

However, usages like:

use rust_i18n::t as the_t_macro;

the_t_macro!("translation key");

and

let translation_key = "translation key";
rust_i18n::t!(translation_key);

need us to do more things with the AST.

@SteveLauC
Copy link

I gave this a go today, in Topgrade's locale file checker:

$ ./topgrade_i18n_locale_file_checker --locale-file ../topgrade/locales/app.yml --rust-src-to-check ../topgrade/src
Errors Found:
  UseOfKeysDoNotExist
    file '../topgrade/src/self_update.rs' / line '52' / column '27' / key 'Respawning...'
    file '../topgrade/src/steps/os/windows.rs' / line '202' / column '21' / key 'Could not find Topgrade in any WSL disribution'
    file '../topgrade/src/steps/os/windows.rs' / line '209' / column '20' / key 'Windows Update'
    file '../topgrade/src/main.rs' / line '488' / column '19' / key '\n(R)eboot\n(S)hell\n(Q)uit'

It works well, though I am not sure if we can do this in build.rs 🤔

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

No branches or pull requests

3 participants