mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-29 18:36:22 +02:00
Feat: TrustGraph i18n & Documentation Translation Updates (#781)
Native CLI i18n: The TrustGraph CLI has built-in translation support that dynamically loads language strings. You can test and use different languages by simply passing the --lang flag (e.g., --lang es for Spanish, --lang ru for Russian) or by configuring your environment's LANG variable. Automated Docs Translations: This PR introduces autonomously translated Markdown documentation into several target languages, including Spanish, Swahili, Portuguese, Turkish, Hindi, Hebrew, Arabic, Simplified Chinese, and Russian.
This commit is contained in:
parent
19f73e4cdc
commit
f95fd4f052
560 changed files with 236300 additions and 99 deletions
40
tests/unit/test_base/test_i18n.py
Normal file
40
tests/unit/test_base/test_i18n.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
from trustgraph.i18n import get_language_pack, get_translator, normalize_language
|
||||
|
||||
|
||||
def test_normalize_language_handles_regions_and_accept_language():
|
||||
assert normalize_language(None) == "en"
|
||||
assert normalize_language("") == "en"
|
||||
|
||||
assert normalize_language("es-ES") == "es"
|
||||
assert normalize_language("pt-BR") == "pt"
|
||||
assert normalize_language("zh") == "zh-cn"
|
||||
|
||||
assert normalize_language("es-ES,es;q=0.9,en;q=0.8") == "es"
|
||||
assert normalize_language("unknown") == "en"
|
||||
|
||||
|
||||
def test_language_pack_loads_from_resources():
|
||||
pack = get_language_pack("en")
|
||||
assert isinstance(pack, dict)
|
||||
|
||||
# Key should exist and map to a non-empty string.
|
||||
title = pack.get("cli.verify_system_status.title")
|
||||
assert isinstance(title, str)
|
||||
assert title.strip() != ""
|
||||
|
||||
|
||||
def test_translator_formats_placeholders():
|
||||
tr = get_translator("en")
|
||||
out = tr.t(
|
||||
"cli.verify_system_status.checking_attempt",
|
||||
name="Pulsar",
|
||||
attempt=2,
|
||||
)
|
||||
|
||||
assert "Pulsar" in out
|
||||
assert "2" in out
|
||||
|
||||
|
||||
def test_translator_falls_back_to_key_for_unknown_keys():
|
||||
tr = get_translator("en")
|
||||
assert tr.t("missing.key") == "missing.key"
|
||||
75
tests/unit/test_gateway/test_endpoint_i18n.py
Normal file
75
tests/unit/test_gateway/test_endpoint_i18n.py
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
"""Tests for Gateway i18n pack endpoint."""
|
||||
|
||||
import json
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from aiohttp import web
|
||||
|
||||
from trustgraph.gateway.endpoint.i18n import I18nPackEndpoint
|
||||
|
||||
|
||||
class TestI18nPackEndpoint:
|
||||
|
||||
def test_i18n_endpoint_initialization(self):
|
||||
mock_auth = MagicMock()
|
||||
|
||||
endpoint = I18nPackEndpoint(
|
||||
endpoint_path="/api/v1/i18n/packs/{lang}",
|
||||
auth=mock_auth,
|
||||
)
|
||||
|
||||
assert endpoint.path == "/api/v1/i18n/packs/{lang}"
|
||||
assert endpoint.auth == mock_auth
|
||||
assert endpoint.operation == "service"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_i18n_endpoint_start_method(self):
|
||||
mock_auth = MagicMock()
|
||||
endpoint = I18nPackEndpoint("/api/v1/i18n/packs/{lang}", mock_auth)
|
||||
await endpoint.start()
|
||||
|
||||
def test_add_routes_registers_get_handler(self):
|
||||
mock_auth = MagicMock()
|
||||
mock_app = MagicMock()
|
||||
|
||||
endpoint = I18nPackEndpoint("/api/v1/i18n/packs/{lang}", mock_auth)
|
||||
endpoint.add_routes(mock_app)
|
||||
|
||||
mock_app.add_routes.assert_called_once()
|
||||
call_args = mock_app.add_routes.call_args[0][0]
|
||||
assert len(call_args) == 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_unauthorized_on_invalid_auth_scheme(self):
|
||||
mock_auth = MagicMock()
|
||||
mock_auth.permitted.return_value = True
|
||||
|
||||
endpoint = I18nPackEndpoint("/api/v1/i18n/packs/{lang}", mock_auth)
|
||||
|
||||
request = MagicMock()
|
||||
request.path = "/api/v1/i18n/packs/en"
|
||||
request.headers = {"Authorization": "Token abc"}
|
||||
request.match_info = {"lang": "en"}
|
||||
|
||||
resp = await endpoint.handle(request)
|
||||
assert isinstance(resp, web.HTTPUnauthorized)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_returns_pack_when_permitted(self):
|
||||
mock_auth = MagicMock()
|
||||
mock_auth.permitted.return_value = True
|
||||
|
||||
endpoint = I18nPackEndpoint("/api/v1/i18n/packs/{lang}", mock_auth)
|
||||
|
||||
request = MagicMock()
|
||||
request.path = "/api/v1/i18n/packs/en"
|
||||
request.headers = {}
|
||||
request.match_info = {"lang": "en"}
|
||||
|
||||
resp = await endpoint.handle(request)
|
||||
|
||||
assert resp.status == 200
|
||||
payload = json.loads(resp.body.decode("utf-8"))
|
||||
assert isinstance(payload, dict)
|
||||
assert "cli.verify_system_status.title" in payload
|
||||
Loading…
Add table
Add a link
Reference in a new issue