Skip to content

fix: Replace with a supported robust solution#2

Closed
Xanoutas wants to merge 1 commit into
tari-project:mainfrom
Xanoutas:fix/issue-1-auto
Closed

fix: Replace with a supported robust solution#2
Xanoutas wants to merge 1 commit into
tari-project:mainfrom
Xanoutas:fix/issue-1-auto

Conversation

@Xanoutas

@Xanoutas Xanoutas commented Apr 8, 2026

Copy link
Copy Markdown

Fix for #1: Replace with a supported robust solution

# i18n_checker.py
import os
import json
import difflib

def check_missing_translations(source_locale_path, target_locale_path):
    with open(source_locale_path, 'r', encoding='utf-8') as f:
        source_locale = json.load(f)
    
    with open(target_locale_path, 'r', encoding='utf-8') as f:
        target_locale = json.load(f)
    
    missing_translations = []
    for key in source_locale:
        if key not in target_locale:
            missing_translations.append(key)
    
    return missing_translations

# i18n_translator.py
import openai

def translate_missing_translations(missing_translations, source_locale_path):
    with open(source_locale_path, 'r', encoding='utf-8') as f:
        source_locale = json.load(f)
    
    translations = {}
    for key in missing_translations:
        prompt = f"Translate the following text to the target language: {source_locale[key]}"
        response = openai.Completion.create(
            engine="text-davinci-003",
            prompt=prompt,
            max_tokens=60
        )
        translations[key] = response.choices[0].text.strip()
    
    return translations

# i18n_patch_locales.py
import json

def patch_locales(target_locale_path, translations):
    with open(target_locale_path, 'r', encoding='utf-8') as f:
        target_locale = json.load(f)
    
    target_locale.update(translations)
    
    with open(target_locale_path, 'w', encoding='utf-8') as f:
        json.dump(target_locale, f, ensure_ascii=False, indent=4)

# i18n_qa.py
import difflib

def qa_translations(source_locale_path, target_locale_path):
    with open(source_locale_path, 'r', encoding='utf-8') as f:
        source_locale = json.load(f)
    
    with open(target_locale_path, 'r', encoding='utf-8') as f:
        target_locale = json.load(f)
    
    qa_results = []
    for key in source_locale:
        if key in target_locale:
            diff = difflib.ndiff(source_locale[key], target_locale[key])
            qa_results.append((key, '\n'.join(diff)))
    
    return qa_results

Explanation:

  1. i18n_checker.py: This script checks for missing translations by comparing the source locale file with the target locale file.
  2. i18n_translator.py: This script translates the missing translations using OpenAI's API.
  3. i18n_patch_locales.py: This script patches the target locale file with the translated strings.
  4. i18n_qa.py: This script performs a quality assurance check by comparing the source and target locale files.

These changes replace the hardcoded paths and manual workflow with a more robust solution that can be integrated into CI and supports multiple projects.


Closes #1

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a Python script for internationalization (i18n) management, including functions to check for missing translations, translate them using OpenAI, patch locale files, and perform quality assurance. The review identifies several critical areas for improvement: the translation function lacks a target language parameter and uses deprecated OpenAI APIs, the missing translation checker and locale patcher do not correctly handle nested JSON structures, and the QA function uses an ineffective difflib.ndiff approach that can lead to errors. Additionally, the script needs proper handling for loading the OpenAI API key.

Comment thread fix_1.py
# i18n_translator.py
import openai

def translate_missing_translations(missing_translations, source_locale_path):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The translate_missing_translations function is missing a parameter to specify the target language. Without this, the prompt sent to the AI (line 29) is ambiguous, and the model will not know which language to translate the text into.

Comment thread fix_1.py
Comment on lines +30 to +35
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=60
)
translations[key] = response.choices[0].text.strip()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The openai.Completion API and the text-davinci-003 model are deprecated and no longer functional. Since the project uses openai version ^1.50.2, you should use the ChatCompletion API with a modern model like gpt-4o-mini. Note that the response structure also differs in the new SDK.

Suggested change
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=60
)
translations[key] = response.choices[0].text.strip()
client = openai.OpenAI()
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)
translations[key] = response.choices[0].message.content.strip()

Comment thread fix_1.py
Comment on lines +14 to +16
for key in source_locale:
if key not in target_locale:
missing_translations.append(key)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This logic only identifies missing keys at the root level of the JSON object. Localization files frequently use nested structures (e.g., "errors": { "not_found": "..." }). This implementation will fail to detect missing keys within those nested objects, leading to incomplete translation checks.

Comment thread fix_1.py
with open(target_locale_path, 'r', encoding='utf-8') as f:
target_locale = json.load(f)

target_locale.update(translations)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The update() method performs a shallow merge. If the translation keys represent nested paths, this will overwrite existing nested dictionaries instead of merging the new values into them. For a robust solution, a recursive merge is necessary to preserve the structure of the locale files.

Comment thread fix_1.py
return missing_translations

# i18n_translator.py
import openai

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The script does not load environment variables (e.g., using python-dotenv), which are typically used to manage the OPENAI_API_KEY. Without this or an explicit API key configuration, the OpenAI client will fail to authenticate. Consider adding load_dotenv() if you intend to use environment variables.

Comment thread fix_1.py
Comment on lines +64 to +65
diff = difflib.ndiff(source_locale[key], target_locale[key])
qa_results.append((key, '\n'.join(diff)))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using difflib.ndiff to compare source text with its translation is not an effective QA practice for i18n. Since the languages differ, the diff will essentially show that the entire string has changed, which doesn't help in identifying quality issues. Furthermore, if the values are nested dictionaries, ndiff will raise a TypeError.

@metalaureate

Copy link
Copy Markdown
Collaborator

Closing this in favor of PR #3, which provides a complete Tolgee integration with CI workflows. Thanks for taking a look! 💜

0xPepeSilvia added a commit to 0xPepeSilvia/aiteen that referenced this pull request Apr 14, 2026
74 tests covering: nested-key detection (the case competitor PR tari-project#2 missed),
placeholder preservation, deep-merge correctness, QA placeholder mismatch
reporting, retry logic, and missing-API-key error handling.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace with a supported robust solution

2 participants