Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
74 changes: 74 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: CI

on:
push:
branches: [main]
pull_request:

permissions:
contents: read

concurrency:
group: ci-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install the pinned formatter
shell: bash
run: |
version=$(python3 -c 'import tomllib; print(tomllib.load(open("mise.toml", "rb"))["tools"]["rust"]["version"])')
rustup toolchain install "$version" --profile minimal --component rustfmt
rustup override set "$version"
- name: Check formatting
run: |
cargo fmt --all
git diff --exit-code > "$RUNNER_TEMP/rustfmt.patch"
- name: Keep formatting corrections on failure
if: failure()
uses: actions/upload-artifact@v4
with:
name: rustfmt-patch
path: ${{ runner.temp }}/rustfmt.patch
retention-days: 7
if-no-files-found: ignore
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install the pinned Rust toolchain
shell: bash
run: |
version=$(python3 -c 'import tomllib; print(tomllib.load(open("mise.toml", "rb"))["tools"]["rust"]["version"])')
rustup toolchain install "$version" --profile minimal --component clippy
rustup override set "$version"
- name: Check translations
run: python3 scripts/check-i18n.py
- name: Run tests
run: |
set -o pipefail
cargo test --locked 2>&1 | tee "$RUNNER_TEMP/tests.log"
- name: Check lints
run: |
set -o pipefail
cargo clippy --locked --all-targets --all-features -- -D warnings 2>&1 | tee "$RUNNER_TEMP/clippy.log"
- name: Verify source package
run: cargo package --locked
- name: Keep check logs
if: always()
uses: actions/upload-artifact@v4
with:
name: check-logs
path: ${{ runner.temp }}/*.log
retention-days: 7
if-no-files-found: ignore
- name: Keep the checked source package
uses: actions/upload-artifact@v4
with:
name: lmm-cli-source-${{ github.sha }}
path: target/package/*.crate
retention-days: 7
if-no-files-found: error
5 changes: 5 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ fixtures instead of real account, system configuration or production writes.
Run fmt, clippy and the complete CLI tests locally. Report the tested commit and
skipped checks. A source change does not authorize publishing a crate, release
or production deployment.

Keep user-facing text in locales/en.json and locales/zh-CN.json, not Rust source.
Use stable message keys and named placeholders. Do not translate command names,
JSON field names, identifiers or protocol values. Run scripts/check-i18n.py and
read docs/i18n.md when adding messages. Keep both README versions in sync.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ homepage = "https://api.lmm.best"
readme = "README.md"
keywords = ["lmm", "cli", "oauth", "ai"]
categories = ["command-line-utilities"]
include = ["Cargo.toml", "Cargo.lock", "README.md", "LICENSE", "src/**", "tests/**", "docs/**", "NOTICE", "third_party/**", "scripts/*.py"]
include = ["Cargo.toml", "Cargo.lock", "README.md", "README.zh-CN.md", "locales/**", "mise.toml", "LICENSE", "src/**", "tests/**", "docs/**", "NOTICE", "third_party/**", "scripts/*.py"]

[lib]
name = "lmm"
Expand Down
263 changes: 263 additions & 0 deletions locales/en.json

Large diffs are not rendered by default.

263 changes: 263 additions & 0 deletions locales/zh-CN.json

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions scripts/check-i18n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python3
"""Check embedded translations with only the Python standard library."""
from __future__ import annotations

import json
import re
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
HAN = re.compile("[\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\U00020000-\U0003134f]")
PLACEHOLDER = re.compile(r"\{([a-zA-Z][a-zA-Z0-9_-]*)\}")
CALL_KEY = re.compile(r'(?<![\w.])(?:msg|text|english)\("([^"\n]+)"\)')
MATCH_KEY = re.compile(r'Self::\w+\s*=>\s*"([a-z]+\.[a-z_]+)"')


def unique_object(pairs: list[tuple[str, object]]) -> dict[str, object]:
result: dict[str, object] = {}
for key, value in pairs:
if key in result:
raise ValueError(f"duplicate translation key: {key}")
result[key] = value
return result


def placeholders(value: str) -> list[str]:
remainder = PLACEHOLDER.sub("", value)
if "{" in remainder or "}" in remainder:
raise ValueError(f"invalid placeholder syntax: {value!r}")
return sorted(PLACEHOLDER.findall(value))


def main() -> None:
catalogs = {
path.stem: json.loads(path.read_text(encoding="utf-8"), object_pairs_hook=unique_object)
for path in sorted((ROOT / "locales").glob("*.json"))
}
reference = catalogs["en"]
for locale, catalog in catalogs.items():
if set(catalog) != set(reference):
raise ValueError(f"{locale}: missing={set(reference) - set(catalog)}, extra={set(catalog) - set(reference)}")
for key, value in catalog.items():
if not isinstance(value, str) or not value.strip():
raise ValueError(f"{locale}.{key}: expected a nonempty string")
if placeholders(value) != placeholders(reference[key]):
raise ValueError(f"{locale}.{key}: placeholders differ from English")
used: set[str] = set()
for path in (ROOT / "src").rglob("*.rs"):
source = path.read_text(encoding="utf-8")
if HAN.search(source):
raise ValueError(f"{path.relative_to(ROOT)}: move Chinese UI text into locales/")
used.update(CALL_KEY.findall(source))
used.update(MATCH_KEY.findall(source))
unknown = used - set(reference)
unused = set(reference) - used
if unknown or unused:
raise ValueError(f"unknown message keys={unknown}; unused message keys={unused}")
print(f"i18n: {len(catalogs)} catalogs, {len(reference)} keys; placeholders and runtime references verified")


if __name__ == "__main__":
main()
Loading