5 个版本
0.0.1-alpha.5 | 2023 年 5 月 30 日 |
---|---|
0.0.1-alpha.4 | 2023 年 4 月 19 日 |
0.0.1-alpha.3 | 2023 年 4 月 15 日 |
0.0.1-alpha.2 | 2023 年 4 月 10 日 |
0.0.1-alpha.1 | 2023 年 4 月 3 日 |
在 国际化(i18n) 中排名 #285
每月下载量 24
用于 4 crate
41KB
961 行
glossa-codegen
使用代码生成器生成代码。
它可以在编译时为代码生成常量语言本地化映射
特性
- yaml
- 默认启用。
- 默认文件扩展名为 ".yaml" 或 ".yml"
- ron
- 默认扩展名为 ".ron"
- toml
- 扩展名为 ".toml"
- json
- 扩展名: ".json"
- highlight
除了 highlight,它对应于不同类型的配置。您可以根据需要启用所有功能或添加它们。
默认情况下,文件类型基于文件名后缀确定,并且 映射名称(表名称)基于文件名设置。是否需要在编译时进行反序列化由启用的功能确定。
准备工作
在编写 build.rs
之前,我们需要准备本地化资源文件(本地化文件)。
de (Deutsch, Lateinisch, Deutschland)
- "assets/l10n/de/error.yaml"
text-not-found: Kein lokalisierter Text gefunden
en (English, Latin, United States)
- "assets/l10n/en/error.yaml"
text-not-found: No localized text found
en-GB (English, Latin, Great Britain)
assets/l10n/en-GB/error.yaml
text-not-found: No localised text found
es (español, latino, España)
assets/l10n/es/error.yaml
text-not-found: No se encontró texto localizado
pt (português, latim, Brasil)
assets/l10n/pt/error.yaml
text-not-found: Nenhum texto localizado encontrado
build.rs
use glossa_codegen::{consts::*, prelude::*};
use std::{io, path::PathBuf};
fn main() -> io::Result<()> {
// Specify the version as the current package version to avoid repetitive compilation for the same version.
let ver = get_pkg_version!();
// This is a constant array: ["src", "assets", "localisation.rs"], which is converted into a path for storing automatically generated Rust code related to localisation.
// path: "src/assets/localisation.rs".
let rs_path = PathBuf::from_iter(default_l10n_rs_file_arr());
// If it's the same version, then exit.
if is_same_version(&rs_path, Some(ver))? {
// When developing, we can comment out the `return` statement below so that every change will be recompiled and won't exit prematurely.
return Ok(());
}
// If the path is "src/assets/localisation.rs", then it will append `mod localisation;` and related `use` statements to "src/assets/mod.rs".
append_to_l10n_mod(&rs_path)?;
// A new file will be created here:
// - Linux(non android): "/dev/shm/localisation.tmp"
// - Other:"src/assets/localisation.tmp"
// After the code generation is complete, rename(move) the file: "/dev/shm/localisation.tmp" -> "src/assets/localisation.rs".
// Note: If written directly to `rs_path` and `cargo` is interrupted during building, it may result in incomplete generated code. Therefore, `tmp_path` is used as a temporary buffer file.
let tmp_path = get_shmem_path(&rs_path)?;
let writer = MapWriter::new(&tmp_path, &rs_path);
// default_l10n_dir_arr() is also a constant array: ["assets", "l10n"].
// If the current localisation resource path is at the parent level, then you can use `path = PathBuf::from_iter([".."].into_iter().chain(default_l10n_dir_arr()));`.
let l10n_path = PathBuf::from_iter(default_l10n_dir_arr());
let generator = Generator::new(l10n_path).with_version(ver);
// Invoke the generator here to generate code.
generator.run(writer)
}
依赖项
~2–11MB
~115K SLoC