-
Notifications
You must be signed in to change notification settings - Fork 32
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
Introduced the minify_key
feature for i18n!
and added support for format specifiers in t!
#73
Conversation
a9f2014
to
0ab5a22
Compare
我代码拉下来看了一下。 看起来 这种情况下为什么还需要 我的意思是,如果想改进自动提取,我们可以试着让 |
还是有区别的, 另外当前业界 GNU 系的 gettext 用的是 而 增加 |
Benchmark:
|
crates/cli/src/main.rs
Outdated
@@ -27,6 +29,22 @@ struct I18nArgs { | |||
/// Extract all untranslated I18n texts from source code | |||
#[arg(default_value = "./")] | |||
source: Option<String>, | |||
/// Add a translation to the localize file for `tr!` | |||
#[arg(long, default_value = None, name = "TEXT")] | |||
tr: Option<Vec<String>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--tr
这个参数怎么使用的?这个 TEXT
是传什么,可以在 README.md 后面 CLI 部分补充一下例子。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个参数目前传的是要翻译的原文,目的是用来将一些没在代码里存在文本(例如:运行时从其他地方获取的或者是动态生成的),添加到 locale 文件里面,因为 tr_key
是要通过计算才能得出的。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个如果目前用不用到的话,可以先不加吗?避免不常用的功能以后很难维护。其实我之前设计 rust-i18n 的时候,就是因为目前 Rust 社区其他的 I18n 工具都太复杂了。
#[proc_macro] | ||
pub fn key(input: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
pub fn vakey(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not keep key
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
key
的含义比较泛化,在没有 tr!
之前是没问题的,但是在加了 tr!
之后,就可能会与 tr_key
产生混淆。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那为啥用 vakey 这个比较奇怪。这里是已有的 pub 函数,我不想做不必要的改动。
以 t 为主要的功能,所以叫 key 也没问题的。
确实简化 key 挺有必要的,例如 wasm 的场景,应该可以减少 wasm 的体积。 就目前的改动,我感觉或许可以应用到 为 i18n 初始化新增
rust_i18n::i18n!("locales", minify_key = true) 这样的话,就可以在
|
Signed-off-by: Varphone Wong <[email protected]>
…d assignment For example: tr!("Hello, %{name}", name: "world");
…on for tr! Usage: cargo i18n --tr "Hello, world"
tr!
to get translation with literalsminify_key
feature for i18n!
and added support for format specifiers in t!
d380b27
to
952c7ff
Compare
|
||
// Configuration using the `[package.metadata.i18n]` section in `Cargo.toml`, | ||
// Useful for the `cargo i18n` command line tool. | ||
i18n!(metadata = true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
从 [package.metadata.i18n]
读取配置这个我老早就想做了。
不过我觉得这个可以不用增加参数,默认就会读配置,按优先级处理,i18n!
如果有参数,会覆盖配置。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里改一下,让 metadata 默认就是 true
不用传
) { | ||
let I18nAttrs { | ||
let I18nConfig { | ||
minify_key, | ||
minify_key_len, | ||
minify_key_prefix, | ||
minify_key_thresh, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你先不要暴露 minify_key_len
, minify_key_prefix
, minify_key_thresh
几个参数,不一定非得需要。等后面有需要的时候再说。以免后面没想好有 API 改动。
另外我倒是觉得这么多参数,不如让 miniify_key
支持 true 或一个函数,如果是函数,由使用者自行实现,这样可定制度更高。我们只需要给出一个默认的实现就好了。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这几个参数没有暴露给最终用户,但是生成本地化文件时是需要的,不然没法和代码里的配置相一致。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用函数给用户定制的话有几个问题:
- 传进来的在编译期无法执行,造成无法在编译期生成短键。
- 如果用户可以自己定制短键生成算法,那就会造成标准不统一,生成的本地化文件就无法通用,影响生态的发展;如果用都同一个算法,大家只是在长度、前缀上做些调整,这样要引用他人项目里的已经翻译好的文件过来也是比较简单的。
- 命令行工具 rust-i18n-cli 也是需要这些参数来扫出要翻译的内容及生成短键,如果用函数来定制,那也没法放在 metadata 里面给程序读取。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那就留着
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不过长度有没有必要提供,我觉得可以想好以后,给一个方案,这里参数稍微一改动,原来的国际化就对不上了(并且人工对也很难对应上)
你再调整一下,其他我觉得都可以了。 |
我先合并了,我说的问题,我来稍微改改 |
- Renamed `mikey` to `minify_key`. - Removed unused `vakey`. - Removed `t!((key, msg))` support, this need to think about it. - Hidden (mark `#[doc(hidden)]) some private method, even it pub, it only provided for rust_i18n itself.
What's New:
minify_key
attribute to thei18n!
macro to support short hashed keys, optimizing memory usage and lookup speed.metadata
attribute to thei18n!
macro to load configuration from the[package.metadata.i18n]
section inCargo.toml
, similar torust-i18n-cli
.t!
macro. You can now usestd::fmt
syntax to format variables.Add
tkv!
macro to generates a translation key and corresponding value pair from a given input value.Added
-t
option torust-i18n-cli
to manually add a translation to the localization file.log-miss-tr
feature to log missing translations at the warning level. This feature requires thelog
crate.