I built a localization library with compile-time checks, no_std, and O(1) complexity

While working on a GUI application, i started thinking about supporting multiple languages. During that process, i came up with the idea of storing the current language as a static variable, and localized expressions as arrays. At the usage site, the program simply indexes into the array and retrieves the expression corresponding to the selected language. I liked this approach, so i implemented it as a separate crate. You can find more details here: crates.io: Rust Package Registry.

This is my first public library, so i'd really appreciate any feedback and suggestions.

1 Like

Overall, I'd say it is pretty good. Just a couple of notes:

  • In the docs, it isn't immediately clear that the init_localemacro generates a Locale enum. Where you use init_locale and init_locale_with_storage, perhaps you can add a comment saying that this is where the Locale enum you use later comes from
  • I haven't looked at it that much, but the way your are transmuting the Locale enum might be undefined behaviour. Since you are already using a macro, perhaps you could use it to implement the From trait for the Locale struct.
  1. I added explanations for localize! and expression!(commit) and will include them in the next update. The init_locale! macro docs contain information about what is being generated, including the Locale enum.
  2. Today, I implemented the functionality to convert usize to Locale and vice versa (commit). Regarding the use of transmute: it is used only when the usize value is guaranteed to be within the bounds of the Locale variants. For other cases, the TryFrom trait implemented in the same commit can be used.

Thanks for the feedback.

No need for unsafe, a simple match (that can be generated by the macro) will compile to the exact same code: Compiler Explorer

1 Like

I considered match and transmute. I chose transmute because it shorter.

However, i really should change it to match. I'll do it tomorrow.

1 Like

I considered match and transmute. I chose transmute because it shorter.

However, i really should change it to match. I'll do it tomorrow.

UPD: released version 1.2.3, which lacks unsafe code

2 Likes