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

Implement validation #3

Merged
merged 31 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1a18624
feat: parse strings
frosch123 Apr 14, 2024
2ddc356
feat: recompile parsed strings
frosch123 Apr 14, 2024
a57647a
refactor: move not-parser-related stuff to validate.rs
frosch123 Apr 14, 2024
6bf7679
feat: add string command infos
frosch123 Apr 14, 2024
87ae2dc
fix: accept all whitespace as separator in commands
frosch123 Apr 16, 2024
517dd7f
refactor: properly name the fragment content
frosch123 Apr 16, 2024
73ecc2a
refactor: make everything public
frosch123 Apr 16, 2024
55b910a
feat: sanitize_whitespace
frosch123 Apr 16, 2024
b6435fd
refactor: use fancy-pants syntax
frosch123 Apr 20, 2024
ff78607
refactor: make everything public
frosch123 Apr 20, 2024
9fc96bc
fix: command names can have underscores
frosch123 Apr 20, 2024
bd4c555
feat: compute signature of base strings
frosch123 Apr 20, 2024
bd8733a
feat: need language file dialect for validation
frosch123 Apr 20, 2024
0699a23
fix: unify punctuation
frosch123 Apr 21, 2024
32e146a
fix: use ordered maps for deterministic error messages
frosch123 Apr 21, 2024
b322706
feat: string validation
frosch123 Apr 21, 2024
d430c83
feat: string normalization
frosch123 Apr 21, 2024
f030414
refactor: turn allow_cases/genders into methods
frosch123 Apr 23, 2024
ae90952
refactor: parse Dialect using traits
frosch123 Apr 23, 2024
78812e9
feat: add high-level glue
frosch123 Apr 23, 2024
309dea7
doc: usage
frosch123 Apr 23, 2024
f8d6c29
fix: report position for parser errors.
frosch123 Apr 24, 2024
a62423b
feat: report error positions as range
frosch123 Apr 25, 2024
86aec9d
feat: report error positions as unicode codepoint offsets
frosch123 Apr 25, 2024
bc4c8de
fix: only accept 'default' as name for the default case
frosch123 Apr 25, 2024
b711b99
feat: serialize error severity as string
frosch123 Apr 25, 2024
8083f1c
feat: put the fix-suggestion on the same line as the error message
frosch123 Apr 25, 2024
2fa2cc5
doc: update example output and fix typos
frosch123 Apr 25, 2024
d62c6b5
Merge remote-tracking branch 'truebrain/main' into wip
frosch123 Apr 25, 2024
8419000
doc: typo in first sentence
frosch123 Apr 25, 2024
94cfee1
refactor: cargo fmt
frosch123 Apr 25, 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
45 changes: 45 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
clap = { version = "4.5", features = ["derive" ]}
console_error_panic_hook = "0.1"
regex = "1.10.4"
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
wasm-bindgen = "0.2"
Expand Down
89 changes: 85 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ Have Rust [installed](https://www.rust-lang.org/tools/install).

For easy local development:

```bash
cargo run -- <base> <case> <translation>
```
* Validate base string:
```bash
cargo run -- <base>
```
* Validate translation string:
```bash
cargo run -- <base> <translation>
```

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

## WASM integration

Expand All @@ -26,3 +31,79 @@ For this [wasm-pack](https://rustwasm.github.io/wasm-pack/) is used.
```bash
wasm-pack build --release
```

## API usage

### Step 1: Validate and normalize the base string

**API method:**
```rust
fn validate_base(config: LanguageConfig, base: String) -> ValidationResult
```

**Input:**
* `config.dialect`: One of `openttd`, `newgrf`, `game-script`.
* `config.cases`: Empty for base language.
* `config.genders`: Empty for base language.
* `config.plural_count`: `2` for base language.
* `base`: Base string to validate

**Output:**
* `errors`: List of errors. If this is not empty, the string should not be offered to translators.
* `normalized`: The normalized text is the text, which should be displayed to translators.
frosch123 marked this conversation as resolved.
Show resolved Hide resolved
* In the normalized text, string commands like `RAW_STRING`, `STRING5`, ... are replaced with `STRING`.
* Translators can copy the normalized text as template for their translation.

**Example:**
```console
>>> cargo run "{BLACK}Age: {LTBLUE}{STRING2}{BLACK} Running Cost: {LTBLUE}{CURRENCY}/year"
ERROR at byte 61: Unknown string command '{CURRENCY}'.
frosch123 marked this conversation as resolved.
Show resolved Hide resolved

>>> cargo run "{BLACK}Age: {LTBLUE}{STRING2}{BLACK} Running Cost: {LTBLUE}{CURRENCY_LONG}/year"
NORMALIZED:{BLACK}Age: {LTBLUE}{0:STRING}{BLACK} Running Cost: {LTBLUE}{1:CURRENCY_LONG}/year
```

### Step 2: Translators enter stuff
frosch123 marked this conversation as resolved.
Show resolved Hide resolved

* Translators must provide a text for the default case.
* Other cases are optional.
* Game-scripts do not support cases. There is a method in `LanguageConfig` to test for this, but it is not exported yet.

### Step 3: Validate and normalize the translation string

**API method:**
```rust
fn validate_translation(config: LanguageConfig, base: String, case: String, translation: String) -> ValidationResult
```

**Input:**
* `config.dialect`: One of `openttd`, `newgrf`, `game-script`.
* `config.cases`: `case` from `nile-config`.
* `config.genders`: `gender` from `nile-config`.
* `config.plural_count`: Number of plural forms from `nile-config`.
* `base`: Base string the translation is for.
* `case`: Case for the translation. `""` and `"default"` are both valid for the default case.
frosch123 marked this conversation as resolved.
Show resolved Hide resolved
* `translation`: The text entered by the translator.

**Output:**
* `errors`: List of errors.
* `critical`: Severity of the error.
frosch123 marked this conversation as resolved.
Show resolved Hide resolved
* `true`: The translation is broken, and must not be committed to OpenTTD.
* `false`: 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.
* `position`: Byte position in input string. `None`, if general message without location.
frosch123 marked this conversation as resolved.
Show resolved Hide resolved
* `message`: Error message.
* `suggestion`: Some extended message with hints.
* `normalized`: The normalized text is the text, which should be committed. In the normalized text, trailing whitespace and other junk has been sanitized.
frosch123 marked this conversation as resolved.
Show resolved Hide resolved

**Example:**
```console
>>> cargo run "{BLACK}Age: {LTBLUE}{STRING2}{BLACK} Running Cost: {LTBLUE}{CURRENCY_LONG}/year" "{BLUE}Alter: {LTBLUE}{STRING}{BLACK} Betriebskosten: {LTBLUE}{0:CURRENCY_LONG}/Jahr"
ERROR at byte 61: Duplicate parameter '{0:CURRENCY_LONG}'.
ERROR at byte 61: Expected '{0:STRING2}', found '{CURRENCY_LONG}'.
ERROR: String command '{1:CURRENCY_LONG}' is missing.
WARNING: String command '{BLUE}' is unexpected.
HINT: Remove this command.
frosch123 marked this conversation as resolved.
Show resolved Hide resolved

>>> cargo run "{BLACK}Age: {LTBLUE}{STRING2}{BLACK} Running Cost: {LTBLUE}{CURRENCY_LONG}/year" "{BLACK}Alter: {LTBLUE}{STRING}{BLACK} Betriebskosten: {LTBLUE}{CURRENCY_LONG}/Jahr"
NORMALIZED:{BLACK}Alter: {LTBLUE}{0:STRING}{BLACK} Betriebskosten: {LTBLUE}{1:CURRENCY_LONG}/Jahr
```
Loading