#编译时 #区域 #语言 #toml #yaml #编译

simple-i18n

A simple compile time i18n implementation in Rust

4 个版本

0.1.4 2021 年 6 月 7 日
0.1.3 2021 年 6 月 5 日
0.1.2 2021 年 6 月 3 日
0.1.1 2021 年 6 月 3 日
0.1.0 2021 年 6 月 3 日

#308 in 国际化 (i18n)

23 每月下载量

MIT 许可证

18KB
135

i18n-rs

Crates.io Lines Crates.io docs.rs rust-reportcard dependency status

A simple compile time i18n implementation in Rust.

This is a personal project. If you need a stable and powerful i18n library, you may need fluent.

If you think this crate is not easy to use, I found another similar crate: https://github.com/terry90/internationalization-rs

使用

In crates.io, the name of this package is simple-i18n, because the name of i18n-rs is occupied by an empty crate. shit...

Add simple-i18n = "0.1" to Cargo.toml

示例

Look i18n-example

cd examples/i18n-example
LOCALE_PATH=locale cargo run --package i18n-example --bin i18n-example

文档

docs.rs

仓库

i18n-rs will load your locale (toml, json or yaml) into the code during compilation. Then you can use lang! (to switch the locale) and use i18n to get the text.

LOCALE_PATH

The LOCALE_PATH environment variable is used to find your locale file.

Please note that because the dependent library cannot get the current path where you actually run the build command during compilation, the safest method is actually to use the absolute path.

Usually we will store locale files in the locale directory in the project root directory and set LOCALE_PATH to locale.

The current behavior is

i18n-rs will find ${workspace_root}/$LOCALE_PATH in OUT_DIR or ./ using cargo metadata.

In other words, if LOCALE_PATH is a relative path, it should be based on the workspace_root of the project, not the user's current path.

And it is best to run the build command in the project root directory.

Locale files

本地化文件支持 jsontomlyaml

您可以使用单个文件或使用一个文件夹。如果是单个文件,语言代码是文件名。

如果是文件夹,文件夹名是语言代码,子文件夹和文件名将用作字段名。

您可以在文件名前添加 . 以避免成为字段名。

内容将被展平,键将通过 . 连接起来成为字段名。

示例

{
    "words": {
        "greetings": {
            "hi": "Hi!"
        }
    }
}

equal

{
    "words.greetings.hi": "Hi!"
}

严格和宽松

默认情况下,将使用严格检查。

在宽松模式下,如果您尝试获取不存在的字段或不存在的地方,将返回字段本身。

但严格模式将检查您的输入 lang!i18n! 以确保您正在使用现有的本地化和所有本地化中存在的字段。

如果出现错误,将返回 panic!

请放心,所有这些都是在编译时检查的,因此严格检查几乎不会影响运行时性能,并且在运行时不会出现恐慌。

注意:因为它需要在编译时检查,所以在严格模式下必须使用字符串字面量。

幸运的是,我们可以自由地在宽松和严格模式之间切换。例如 i18n!("xxx.x"; loose)

基准测试

strict contrast/no strict
                        time:   [29.048 ns 29.387 ns 29.736 ns]
                        change: [-15.897% -13.053% -10.253%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild
  
strict contrast/strict  time:   [29.108 ns 29.431 ns 29.776 ns]
                        change: [-2.6412% -0.8426% +1.0984%] (p = 0.38 > 0.05)
                        No change in performance detected.
Found 4 outliers among 100 measurements (4.00%)
  2 (2.00%) high mild
  2 (2.00%) high severe

change_lang             time:   [148.38 ns 159.76 ns 178.01 ns]
                        change: [+0.4039% +4.5240% +10.326%] (p = 0.05 > 0.05)
                        No change in performance detected.
Found 5 outliers among 100 measurements (5.00%)
  3 (3.00%) high mild
  2 (2.00%) high severe

依赖项

~4MB
~87K SLoC