Skip to content

Commit

Permalink
Added language translation
Browse files Browse the repository at this point in the history
  • Loading branch information
bram committed Dec 23, 2024
1 parent 076022d commit 3f94342
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
46 changes: 36 additions & 10 deletions python_gpt_po/po_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from dataclasses import dataclass

import polib
import pycountry
from dotenv import load_dotenv
from openai import OpenAI
from pkg_resources import DistributionNotFound, get_distribution
Expand Down Expand Up @@ -58,18 +59,43 @@ def disable_fuzzy_translations(po_file_path):
@staticmethod
def get_file_language(po_file_path, po_file, languages, folder_language):
"""Determines the language for a .po file."""
# Attempt to get language from the file metadata first
file_lang = po_file.metadata.get('Language', '')
normalized_lang = POFileHandler.normalize_language_code(file_lang)

# If the file's language is not valid, infer it from the folder structure
if not file_lang or file_lang not in languages:
if folder_language:
inferred_lang = next((part for part in po_file_path.split(os.sep) if part in languages), None)
if inferred_lang:
logging.info("Inferred language for .po file: %s as %s", po_file_path, inferred_lang)
return inferred_lang
return None
return file_lang
if normalized_lang in languages:
return normalized_lang

if folder_language:
for part in po_file_path.split(os.sep):
norm_part = POFileHandler.normalize_language_code(part)
if norm_part in languages:
logging.info("Inferred language for .po file: %s as %s", po_file_path, norm_part)
return norm_part

return None

@staticmethod
def normalize_language_code(lang):
"""Convert language name or code to ISO 639-1 code."""
# Try direct lookup
if len(lang) == 2:
try:
return pycountry.languages.get(alpha_2=lang.lower()).alpha_2
except AttributeError:
pass

# Try by name
try:
return pycountry.languages.get(name=lang.title()).alpha_2
except AttributeError:
pass

# Try by native name
for language in pycountry.languages:
if hasattr(language, 'inverted_name') and language.inverted_name.lower() == lang.lower():
return language.alpha_2

return None

@staticmethod
def log_translation_status(po_file_path, original_texts, translations):
Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
polib==1.2.0
openai==v1.42.0
openai==1.58.1
python-dotenv==1.0.0
pytest==8.2.2
tenacity==9.0.0
setuptools-scm==8.1.0
setuptools-scm==8.1.0
pycountry==24.6.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
include_package_data=True,
install_requires=[
'polib==1.2.0',
'openai==v1.42.0',
'openai==1.58.1',
'python-dotenv==1.0.0',
'tenacity==9.0.0',
],
Expand Down

0 comments on commit 3f94342

Please sign in to comment.