Skip to content

Commit

Permalink
Refactor code for #73, private some method.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
huacnlee committed Jan 31, 2024
1 parent 37bb5de commit dbc68b9
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 267 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ i18n!("locales", minify_key = true);
i18n!("locales",
minify_key = true,
minify_key_len = 12,
minify_key_prefix = "T.",
minify_key_prefix = "t_",
minify_key_thresh = 64
);
// 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.
// 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);
i18n!();
```

Or you can import by use directly:
Expand Down Expand Up @@ -116,11 +116,11 @@ You can also split the each language into difference files, and you can choise (

```yml
_version: 1
hello: "Hello world"
messages.hello: "Hello, %{name}"
hello: 'Hello world'
messages.hello: 'Hello, %{name}'

# Generate short hashed keys using `minify_key=true, minify_key_thresh=10`
4Cct6Q289b12SkvF47dXIx: "Hello, %{name}"
4Cct6Q289b12SkvF47dXIx: 'Hello, %{name}'
```

Or use JSON or TOML format, just rename the file to `en.json` or `en.toml`, and the content is like this:
Expand Down
8 changes: 4 additions & 4 deletions benches/minify_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn bench_t(c: &mut Criterion) {

c.bench_function("t_with_locale", |b| b.iter(|| t!("hello", locale = "en")));

c.bench_function("tr_with_threads", |b| {
c.bench_function("t_with_threads", |b| {
let exit_loop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let mut handles = Vec::new();
for _ in 0..4 {
Expand All @@ -32,7 +32,7 @@ pub fn bench_t(c: &mut Criterion) {
}
});

c.bench_function("tr_with_args", |b| {
c.bench_function("t_with_args", |b| {
b.iter(|| {
t!(
"Hello, %{name}. Your message is: %{msg}",
Expand All @@ -42,7 +42,7 @@ pub fn bench_t(c: &mut Criterion) {
})
});

c.bench_function("tr_with_args (str)", |b| {
c.bench_function("t_with_args (str)", |b| {
b.iter(|| {
t!(
"Hello, %{name}. Your message is: %{msg}",
Expand All @@ -52,7 +52,7 @@ pub fn bench_t(c: &mut Criterion) {
})
});

c.bench_function("tr_with_args (many)", |b| {
c.bench_function("t_with_args (many)", |b| {
b.iter(|| {
t!(
r#"Hello %{name} %{surname}, your account id is %{id}, email address is %{email}.
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn translate_value_parser(s: &str) -> Result<(String, String), std::io::Error> {
}
}

/// Add translations to the localize file for tr!
/// Add translations to the localize file for t!
fn add_translations(
list: &[(String, String)],
results: &mut HashMap<String, Message>,
Expand Down
8 changes: 4 additions & 4 deletions crates/extract/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn generate<'a, P: AsRef<Path>>(
eprintln!("Writing to {}\n", filename);

let text = convert_text(&trs, format);
write_file(&output_path, &filename, &text)?;
write_file(&output_path, filename, &text)?;

// Finally, return error for let CI fail
let err = std::io::Error::new(std::io::ErrorKind::Other, "");
Expand Down Expand Up @@ -98,7 +98,7 @@ fn generate_result<'a, P: AsRef<Path>>(
};

trs.entry(key.clone())
.or_insert_with(HashMap::new)
.or_default()
.insert(locale.to_string(), value.to_string());
}
}
Expand Down Expand Up @@ -145,7 +145,7 @@ mod tests {
"_version": 2
}
"#;
assert_eq_json(&result, &expect);
assert_eq_json(&result, expect);

trs.insert("hello".to_string(), {
let mut map = HashMap::new();
Expand All @@ -164,7 +164,7 @@ mod tests {
}
}
"#;
assert_eq_json(&result, &expect);
assert_eq_json(&result, expect);

let format = "yaml";
let result = convert_text(&trs, format);
Expand Down

0 comments on commit dbc68b9

Please sign in to comment.