Skip to content

Commit

Permalink
refactor(i18n_codegen): Update Cargo.toml and README.md, adjust code …
Browse files Browse the repository at this point in the history
…generation logic

Update the Cargo.toml file to adjust documentation links and change the version number to 0.2.0.
Update the README.md file to add missing contributors and adjust example code to reflect the latest changes.
Refactor example_path.rs and src/lib.rs to optimize code generation logic, ensuring correct handling of file paths and attributes.
Fix error handling logic in src/lib.rs to improve the accuracy of error messages.
BREAKING CHANGE: This commit introduces significant refactoring and version updates that may impact dependency resolution and code generation functionality. Please ensure thorough testing before upgrading.
  • Loading branch information
kingzcheung committed Aug 30, 2024
1 parent b9bb3b2 commit cad9a11
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 717 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/target
**/*.rs.bk
Cargo.lock
Cargo.lock
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[package]
authors = ["David Pedersen <david.pdrsn@gmail.com>"]
authors = ["David Pedersen <david.pdrsn@gmail.com>","Kingz Cheung <kingzcheung@gmail.com>"]
categories = ["web-programming"]
description = "Safe localization library using code generation"
documentation = "https://docs.rs/i18n_codegen"
edition = "2018"
homepage = "https://github.com/davidpdrsn/i18n_codegen"
documentation = "https://docs.rs/i18nify"
edition = "2021"
homepage = "https://github.com/kingzcheung/i18n_codegen"
keywords = ["web", "i18n", "localization", "translation"]
license = "MIT"
name = "i18n_codegen"
name = "i18nify"
readme = "README.md"
repository = "https://github.com/davidpdrsn/i18n_codegen.git"
version = "0.1.1"
repository = "https://github.com/kingzcheung/i18n_codegen.git"
version = "0.2.0"

[dependencies]
syn = {version="2.0.76", features = ["derive"] }
Expand Down
34 changes: 25 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# [i18n_codegen](https://crates.io/crates/i18n_codegen)
# [i18nify](https://github.com/kingzcheung/i18nify)
[简体中文](./README_CN.md)| English

Internationalization library for Rust based on code generation.

> The original repository https://github.com/davidpdrsn/i18n_codegen was implemented by David Pedersen. However, it has some outdated dependencies and has not been maintained for as long as five years.
By leveraging code generation we are able to prevent common bugs like typos in i18n keys,
missing interpolations, or various mistakes between locales.

It requires a directory with one JSON file per locale. Here is an example with English and
It requires a directory (based on `CARGO_MANIFEST_DIR`) with one JSON file per locale. Here is an example with English and
Danish translations:

```javascript
Expand All @@ -24,18 +27,31 @@ Danish translations:

And in Rust:


```
在 `Rust` 中:
```rust
use i18n_codegen::i18n;
use demo::Internationalize;
i18n!("tests/doc_locales");
mod demo {
use i18nify::I18N;
#[derive(I18N)]
#[i18n(folder = "tests/doc_locales")]
pub struct DocLocale;
fn main() {
assert_eq!("Hello, World!", Locale::En.hello_world());
assert_eq!("Hej, Verden!", Locale::Da.hello_world());
}
assert_eq!("Hello Bob", Locale::En.greeting(Name("Bob")));
assert_eq!("Hej Bob", Locale::Da.greeting(Name("Bob")));
fn main() {
// 基于 Locale 枚举类型获取国际化文本
let hello = demo::Locale::En.hello();
assert_eq!("Hello, World!",hello);
println!("{}",hello);
// 基于 `DocLocale` 实现的`Internationalize` trait 获取国际化文本
let hello = demo::DocLocale.da().hello();
println!("{}",hello);
}
```

You can find more details on <https://docs.rs/i18n_codegen>.
50 changes: 50 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# [i18nify](https://github.com/kingzcheung/i18nify)
简体中文| [English](./README.md)

[i18nify](https://github.com/kingzcheung/i18nify) 是一款基于代码生成的 `Rust` 国际化库。

> 原仓库 [https://github.com/davidpdrsn/i18n_codegen](https://github.com/davidpdrsn/i18n_codegen)[David Pedersen](https://github.com/davidpdrsn) 实现的。然而它有一些老旧的依赖,并且已经长达5年不维护。
通过利用代码生成,我们能够防止代码中的拼写错误、缺少插值或各语言环境之间各种错误的常见问题。

它需要一个目录(目录基于`CARGO_MANIFEST_DIR`),其中每个语言环境有一个 JSON 文件。以下是一个包含英语和汉语翻译的例子:

```javascript
// tests/doc_locales/en.json
{
"hello_world": "Hello, World!",
"greeting": "Hello {name}"
}

// tests/doc_locales/da.json
{
"hello_world": "你好,世界!",
"greeting": "你好, {name}"
}
```
`Rust` 中:
```rust
use demo::Internationalize;

mod demo {
use i18nify::I18N;
#[derive(I18N)]
#[i18n(folder = "tests/locales")]
pub struct DocLocale;

}

fn main() {
// 基于 Locale 枚举类型获取国际化文本
let hello = demo::Locale::En.hello();
assert_eq!("Hello, World!",hello);
println!("{}",hello);

// 基于 `DocLocale` 实现的`Internationalize` trait 获取国际化文本
let hello = demo::DocLocale.da().hello();
println!("{}",hello);
}

```

你可以在 <[文档](https://docs.rs/i18nify)> 获取更多细节。
52 changes: 26 additions & 26 deletions example_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,68 +15,68 @@ pub enum Locale {
#[allow(missing_docs)]
pub struct Type<'a>(pub &'a str);
#[allow(missing_docs)]
pub struct One<'a>(pub &'a str);
#[allow(missing_docs)]
pub struct Two<'a>(pub &'a str);
#[allow(missing_docs)]
pub struct Name<'a>(pub &'a str);
#[allow(missing_docs)]
pub struct One<'a>(pub &'a str);
#[allow(missing_docs)]
pub struct Word<'a>(pub &'a str);
impl Locale {
#[allow(missing_docs)]
pub fn duplicate_placeholders(self, name_: Name<'_>) -> String {
pub fn hello(self) -> String {
match self {
Locale::Da => format!("Hej {name}. Er dit navn {name}?", name = name_.0),
Locale::En => format!("Hey {name}. Is your name {name}?", name = name_.0),
Locale::Da => format!("Hej, Verden!"),
Locale::En => format!("Hello, World!"),
}
}
#[allow(missing_docs)]
pub fn missing_interpolation_da(self, word_: Word<'_>) -> String {
pub fn missing_interpolation_en(self, word_: Word<'_>) -> String {
match self {
Locale::En => format!("en foo {word}", word = word_.0),
Locale::Da => format!("da foo",),
Locale::En => format!("en foo",),
Locale::Da => format!("da foo {word}", word = word_.0),
}
}
#[allow(missing_docs)]
pub fn rust_keyword(self, type_: Type<'_>) -> String {
pub fn hello_newline(self) -> String {
match self {
Locale::Da => format!("hva så {type}", type = type_.0),
Locale::En => format!("yo {type}", type = type_.0),
Locale::Da => format!("Hej\nVerden!"),
Locale::En => format!("Hello\nWorld!"),
}
}
#[allow(missing_docs)]
pub fn hello(self) -> String {
pub fn two_placeholders(self, one_: One<'_>, two_: Two<'_>) -> String {
match self {
Locale::Da => format!("Hej, Verden!"),
Locale::En => format!("Hello, World!"),
Locale::En => format!("en {one} {two}", one = one_.0, two = two_.0),
Locale::Da => format!("da {one} {two}", one = one_.0, two = two_.0),
}
}
#[allow(missing_docs)]
pub fn two_placeholders(self, one_: One<'_>, two_: Two<'_>) -> String {
pub fn rust_keyword(self, type_: Type<'_>) -> String {
match self {
Locale::Da => format!("da {one} {two}", one = one_.0, two = two_.0),
Locale::En => format!("en {one} {two}", one = one_.0, two = two_.0),
Locale::Da => format!("hva så {type}", type = type_.0),
Locale::En => format!("yo {type}", type = type_.0),
}
}
#[allow(missing_docs)]
pub fn greeting(self, name_: Name<'_>) -> String {
pub fn duplicate_placeholders(self, name_: Name<'_>) -> String {
match self {
Locale::En => format!("Hello {name}", name = name_.0),
Locale::Da => format!("Hej {name}", name = name_.0),
Locale::En => format!("Hey {name}. Is your name {name}?", name = name_.0),
Locale::Da => format!("Hej {name}. Er dit navn {name}?", name = name_.0),
}
}
#[allow(missing_docs)]
pub fn missing_interpolation_en(self, word_: Word<'_>) -> String {
pub fn missing_interpolation_da(self, word_: Word<'_>) -> String {
match self {
Locale::En => format!("en foo",),
Locale::Da => format!("da foo {word}", word = word_.0),
Locale::Da => format!("da foo",),
Locale::En => format!("en foo {word}", word = word_.0),
}
}
#[allow(missing_docs)]
pub fn hello_newline(self) -> String {
pub fn greeting(self, name_: Name<'_>) -> String {
match self {
Locale::En => format!("Hello\nWorld!"),
Locale::Da => format!("Hej\nVerden!"),
Locale::En => format!("Hello {name}", name = name_.0),
Locale::Da => format!("Hej {name}", name = name_.0),
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use demo::Internationalize;

mod demo {
use i18n_codegen::I18N;



use i18nify::I18N;
#[derive(I18N)]
#[i18n(folder = "tests/locales")]
pub struct DocLocale;
Expand Down
Loading

0 comments on commit cad9a11

Please sign in to comment.