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

Introduced the minify_key feature for i18n! and added support for format specifiers in t! #73

Merged
merged 39 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ba3af46
Rename key! to vakey!
varphone Jan 20, 2024
903c3d1
Add crate::_rust_i18n_try_translate() to returns None on untranslated
varphone Jan 20, 2024
99ff77b
Add CowStr<'a> and TrKey for new tr!
varphone Jan 20, 2024
53b53b1
Add new `tr!` macro to get translations with string literals supports
varphone Jan 20, 2024
e7e7563
Add `tr!` support to `rust-i18n-extract` and `rust-i18n-cli`
varphone Jan 20, 2024
2184045
Update benchmark and tests for tr!
varphone Jan 20, 2024
479c72c
Add new application example for `tr!`
varphone Jan 20, 2024
fdfc1ff
Update README.md for `tr!`
varphone Jan 20, 2024
24468c3
Update to new rust-i18n::locale() with breaking changes
varphone Jan 20, 2024
2c0af63
Add concurrent testing for tr!
varphone Jan 20, 2024
f5a8322
Add tr_with_args (many-dynamic) and format! (many) benchmarks
varphone Jan 20, 2024
ee5cd8c
examples/app-tr: Cleanup
varphone Jan 20, 2024
310dfc3
Update SEO keywords to improve visibility of our package on crates.io
varphone Jan 20, 2024
22740f1
Add colonsAdd colon assignment syntax for tr!, similar to struct fiel…
varphone Jan 20, 2024
834ec0a
Add new option --tr for rust-i18n-cli to support manual add translati…
varphone Jan 20, 2024
85b33e7
Rename feature `log_tr_dyn` to `log-tr-dyn`
varphone Jan 21, 2024
1827611
Change tr! missing log level to warn
varphone Jan 21, 2024
658c680
Refactor Tr
varphone Jan 21, 2024
20ba421
examples/app-tr: Add log-tr-dyn demos
varphone Jan 21, 2024
0449d7b
Improve the help documentation for the `--tr` option in `rust-i18n-cli`
varphone Jan 21, 2024
8f431d0
Add specifiers support to tr! and remove colons assignment style
varphone Jan 21, 2024
be10786
Add CowStr::as_str()
varphone Jan 28, 2024
ff3ebd9
Rename TrKey to MinifyKey
varphone Jan 28, 2024
3a8111b
Add `minify_key` supports to `in18n!` and `tr!`
varphone Jan 28, 2024
4d78704
Add `minify_key` supports to `t!`
varphone Jan 28, 2024
1be4d47
Add `minify_key` supports to `rust-i18n-cli`
varphone Jan 28, 2024
2a9abe1
Add `minify_key` benchmark and testing
varphone Jan 28, 2024
1fa8630
Add new example: app-minify-key
varphone Jan 28, 2024
1b19e83
Add new example: app-egui
varphone Jan 28, 2024
6cfbef7
Move rust-i18n-cli argument `SOURCE` to last
varphone Jan 28, 2024
ee998bd
Cleanup clippy warnings
varphone Jan 28, 2024
f8d816a
Update README.md
varphone Jan 28, 2024
8b325d3
Rename feature `log-missing` to `log-miss-tr`
varphone Jan 28, 2024
a11224f
Cargo.toml: apply toml format
varphone Jan 28, 2024
0b606cc
Move `I18nConfig` from `rust-i18n-cli` to `rust-i18n-support`
varphone Jan 28, 2024
f33d24d
Display the file path when loading a locale file fails
varphone Jan 28, 2024
2a9e8cf
Add `fallback` option to I18nConfig
varphone Jan 28, 2024
8762c15
Add `metadata` supports to `in18n!`
varphone Jan 28, 2024
17d931d
Remove unexpected files
varphone Jan 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ members = [
"crates/macro",
"examples/app-egui",
"examples/app-load-path",
"examples/app-metadata",
"examples/app-minify-key",
"examples/foo",
]
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ i18n!("locales",
);
// Now, if the message length exceeds 64, the `t!` macro will automatically generate
// a 12-byte short hashed key with a "T." prefix for it, if not, it will use the original.

// Configuration using the `[package.metadata.i18n]` section in `Cargo.toml`,
// Useful for the `cargo i18n` command line tool.
i18n!(metadata = true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[package.metadata.i18n] 读取配置这个我老早就想做了。

不过我觉得这个可以不用增加参数,默认就会读配置,按优先级处理,i18n! 如果有参数,会覆盖配置。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里改一下,让 metadata 默认就是 true 不用传

```

Or you can import by use directly:
Expand Down
3 changes: 0 additions & 3 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ version = "3.0.0"
[dependencies]
anyhow = "1"
clap = { version = "4.1.14", features = ["derive"] }
itertools = "0.11.0"
rust-i18n-support = { path = "../support", version = "3.0.0" }
rust-i18n-extract = { path = "../extract", version = "3.0.0" }
serde = { version = "1", features = ["derive"] }
toml = "0.7.4"

[[bin]]
name = "cargo-i18n"
Expand Down
170 changes: 0 additions & 170 deletions crates/cli/src/config.rs

This file was deleted.

22 changes: 10 additions & 12 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use anyhow::Error;
use clap::{Args, Parser};
use rust_i18n_extract::attrs::I18nAttrs;
use rust_i18n_support::MinifyKey;

use std::{collections::HashMap, path::Path};

use rust_i18n_extract::extractor::Message;
use rust_i18n_extract::{extractor, generator, iter};
mod config;
use rust_i18n_support::{I18nConfig, MinifyKey};
use std::{collections::HashMap, path::Path};

#[derive(Parser)]
#[command(name = "cargo")]
Expand Down Expand Up @@ -71,14 +67,16 @@ fn translate_value_parser(s: &str) -> Result<(String, String), std::io::Error> {
fn add_translations(
list: &[(String, String)],
results: &mut HashMap<String, Message>,
attrs: &I18nAttrs,
cfg: &I18nConfig,
) {
let I18nAttrs {
let I18nConfig {
minify_key,
minify_key_len,
minify_key_prefix,
minify_key_thresh,
Copy link
Member

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 或一个函数,如果是函数,由使用者自行实现,这样可定制度更高。我们只需要给出一个默认的实现就好了。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这几个参数没有暴露给最终用户,但是生成本地化文件时是需要的,不然没法和代码里的配置相一致。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

我说的是这里,先不要把这几个参数暴露出来,先只有 minify_key 一项,后面如果有要定制,可以让这个支持 true 或一个函数。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用函数给用户定制的话有几个问题:

  1. 传进来的在编译期无法执行,造成无法在编译期生成短键。
  2. 如果用户可以自己定制短键生成算法,那就会造成标准不统一,生成的本地化文件就无法通用,影响生态的发展;如果用都同一个算法,大家只是在长度、前缀上做些调整,这样要引用他人项目里的已经翻译好的文件过来也是比较简单的。
  3. 命令行工具 rust-i18n-cli 也是需要这些参数来扫出要翻译的内容及生成短键,如果用函数来定制,那也没法放在 metadata 里面给程序读取。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那就留着

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不过长度有没有必要提供,我觉得可以想好以后,给一个方案,这里参数稍微一改动,原来的国际化就对不上了(并且人工对也很难对应上)

} = attrs;
..
} = cfg;

for item in list {
let index = results.len();
let key = if *minify_key {
Expand All @@ -105,14 +103,14 @@ fn main() -> Result<(), Error> {

let source_path = args.source.expect("Missing source path");

let cfg = config::load(std::path::Path::new(&source_path))?;
let cfg = I18nConfig::load(std::path::Path::new(&source_path))?;

iter::iter_crate(&source_path, |path, source| {
extractor::extract(&mut results, path, source, cfg.attrs.clone())
extractor::extract(&mut results, path, source, cfg.clone())
})?;

if let Some(list) = args.translate {
add_translations(&list, &mut results, &cfg.attrs);
add_translations(&list, &mut results, &cfg);
}

let mut messages: Vec<_> = results.iter().collect();
Expand Down
2 changes: 1 addition & 1 deletion crates/extract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ proc-macro2 = { version = "1", features = ["span-locations"] }
quote = "1"
regex = "1"
rust-i18n-support = { path = "../support", version = "3.0.0" }
serde = { version = "1", features = ["derive"] }
serde = "1"
serde_json = "1"
serde_yaml = "0.8"
syn = { version = "2.0.18", features = ["full"] }
Expand Down
47 changes: 0 additions & 47 deletions crates/extract/src/attrs.rs

This file was deleted.

19 changes: 8 additions & 11 deletions crates/extract/src/extractor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::attrs::I18nAttrs;
use anyhow::Error;
use proc_macro2::{TokenStream, TokenTree};
use quote::ToTokens;
use rust_i18n_support::I18nConfig;
use std::collections::HashMap;
use std::path::PathBuf;

Expand Down Expand Up @@ -39,13 +39,9 @@ pub fn extract(
results: &mut Results,
path: &PathBuf,
source: &str,
attrs: I18nAttrs,
cfg: I18nConfig,
) -> Result<(), Error> {
let mut ex = Extractor {
results,
path,
attrs,
};
let mut ex = Extractor { results, path, cfg };

let file = syn::parse_file(source)
.unwrap_or_else(|_| panic!("Failed to parse file, file: {}", path.display()));
Expand All @@ -57,7 +53,7 @@ pub fn extract(
struct Extractor<'a> {
results: &'a mut Results,
path: &'a PathBuf,
attrs: I18nAttrs,
cfg: I18nConfig,
}

impl<'a> Extractor<'a> {
Expand Down Expand Up @@ -99,12 +95,13 @@ impl<'a> Extractor<'a> {
return;
};

let I18nAttrs {
let I18nConfig {
minify_key,
minify_key_len,
minify_key_prefix,
minify_key_thresh,
} = &self.attrs;
..
} = &self.cfg;
let key: Option<proc_macro2::Literal> = Some(literal);

if let Some(lit) = key {
Expand Down Expand Up @@ -242,7 +239,7 @@ mod tests {
let mut ex = Extractor {
results: &mut results,
path: &"hello.rs".to_owned().into(),
attrs: I18nAttrs::default(),
cfg: I18nConfig::default(),
};

ex.invoke(stream).unwrap();
Expand Down
1 change: 0 additions & 1 deletion crates/extract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod attrs;
pub mod extractor;
pub mod generator;
pub mod iter;
Loading
Loading