Skip to content

Commit 3db12a9

Browse files
authored
feat: implement validation (#3)
1 parent 48ce398 commit 3db12a9

File tree

8 files changed

+3589
-32
lines changed

8 files changed

+3589
-32
lines changed

Cargo.lock

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ crate-type = ["cdylib", "rlib"]
1111
[dependencies]
1212
clap = { version = "4.5", features = ["derive" ]}
1313
console_error_panic_hook = "0.1"
14+
regex = "1.10.4"
1415
serde = { version = "1.0", features = ["derive"] }
1516
serde-wasm-bindgen = "0.4"
1617
wasm-bindgen = "0.2"

README.md

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# nile-library - Library supporting nile
22

3-
This repository contains the libirary that supports OpenTTD's translation tool `nile`.
3+
This repository contains the library that supports OpenTTD's translation tool `nile`.
44

55
This library for example validates if a translation is valid for a given base-string, and converts base-strings into a translatable form.
66

@@ -12,11 +12,16 @@ Have Rust [installed](https://www.rust-lang.org/tools/install).
1212

1313
For easy local development:
1414

15-
```bash
16-
cargo run -- <base> <case> <translation>
17-
```
15+
* Validate base string:
16+
```bash
17+
cargo run -- <base>
18+
```
19+
* Validate translation string:
20+
```bash
21+
cargo run -- <base> <translation>
22+
```
1823

19-
It will output whether the translation is valid, and if not, what was wrong with it.
24+
It will output the normalized string form, and whether the string is valid; and if not, what was wrong with it.
2025

2126
## WASM integration
2227

@@ -26,3 +31,78 @@ For this [wasm-pack](https://rustwasm.github.io/wasm-pack/) is used.
2631
```bash
2732
wasm-pack build --release
2833
```
34+
35+
## API usage
36+
37+
### Step 1: Validate and normalize the base string
38+
39+
**API method:**
40+
```rust
41+
fn validate_base(config: LanguageConfig, base: String) -> ValidationResult
42+
```
43+
44+
**Input:**
45+
* `config.dialect`: One of `openttd`, `newgrf`, `game-script`.
46+
* `config.cases`: Empty for base language.
47+
* `config.genders`: Empty for base language.
48+
* `config.plural_count`: `2` for base language.
49+
* `base`: Base string to validate
50+
51+
**Output:**
52+
* `errors`: List of errors. If this is not empty, the string should not be offered to translators.
53+
* `normalized`: The normalized text to display to translators.
54+
* In the normalized text, string commands like `RAW_STRING`, `STRING5`, ... are replaced with `STRING`.
55+
* Translators can copy the normalized text as template for their translation.
56+
57+
**Example:**
58+
```console
59+
>>> cargo run "{BLACK}Age: {LTBLUE}{STRING2}{BLACK} Running Cost: {LTBLUE}{CURRENCY}/year"
60+
ERROR at position 61 to 71: Unknown string command '{CURRENCY}'.
61+
62+
>>> cargo run "{BLACK}Age: {LTBLUE}{STRING2}{BLACK} Running Cost: {LTBLUE}{CURRENCY_LONG}/year"
63+
NORMALIZED:{BLACK}Age: {LTBLUE}{0:STRING}{BLACK} Running Cost: {LTBLUE}{1:CURRENCY_LONG}/year
64+
```
65+
66+
### Step 2: Translators translates strings
67+
68+
* Translators must provide a text for the default case.
69+
* Other cases are optional.
70+
* Game-scripts do not support cases. There is a method in `LanguageConfig` to test for this, but it is not exported yet.
71+
72+
### Step 3: Validate and normalize the translation string
73+
74+
**API method:**
75+
```rust
76+
fn validate_translation(config: LanguageConfig, base: String, case: String, translation: String) -> ValidationResult
77+
```
78+
79+
**Input:**
80+
* `config.dialect`: One of `openttd`, `newgrf`, `game-script`.
81+
* `config.cases`: `case` from `nile-config`.
82+
* `config.genders`: `gender` from `nile-config`.
83+
* `config.plural_count`: Number of plural forms from `nile-config`.
84+
* `base`: Base string the translation is for.
85+
* `case`: Case for the translation. Use `"default"` for the default case.
86+
* `translation`: The text entered by the translator.
87+
88+
**Output:**
89+
* `errors`: List of errors.
90+
* `severity`: Severity of the error.
91+
* `error`: The translation is broken, and must not be committed to OpenTTD.
92+
* `warning`: The translation is okay to commit, but translators should fix it anyway. This is used for new validations, which Eints did not do. So there are potentially lots of existing translations in violation.
93+
* `position`: Byte position in input string. `None`, if general message without location.
94+
* `message`: Error message.
95+
* `suggestion`: Some extended message with hints.
96+
* `normalized`: The normalized text to committed. In the normalized text, trailing whitespace and other junk has been removed.
97+
98+
**Example:**
99+
```console
100+
>>> cargo run "{BLACK}Age: {LTBLUE}{STRING2}{BLACK} Running Cost: {LTBLUE}{CURRENCY_LONG}/year" "{BLUE}Alter: {LTBLUE}{STRING}{BLACK} Betriebskosten: {LTBLUE}{0:CURRENCY_LONG}/Jahr"
101+
ERROR at position 61 to 78: Duplicate parameter '{0:CURRENCY_LONG}'.
102+
ERROR at position 61 to 78: Expected '{0:STRING2}', found '{CURRENCY_LONG}'.
103+
ERROR: String command '{1:CURRENCY_LONG}' is missing.
104+
WARNING: String command '{BLUE}' is unexpected. HINT: Remove this command.
105+
106+
>>> cargo run "{BLACK}Age: {LTBLUE}{STRING2}{BLACK} Running Cost: {LTBLUE}{CURRENCY_LONG}/year" "{BLACK}Alter: {LTBLUE}{STRING}{BLACK} Betriebskosten: {LTBLUE}{CURRENCY_LONG}/Jahr"
107+
NORMALIZED:{BLACK}Alter: {LTBLUE}{0:STRING}{BLACK} Betriebskosten: {LTBLUE}{1:CURRENCY_LONG}/Jahr
108+
```

0 commit comments

Comments
 (0)