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

Fix skipping already translated messages #2

Merged
merged 1 commit into from
Oct 6, 2023
Merged
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
27 changes: 13 additions & 14 deletions django_deep_translator/management/commands/translate_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import polib
from django.conf import settings
from django.core.management.base import BaseCommand
# from deep_translator import GoogleTranslator
from django_deep_translator.services import GoogleTranslatorService
from django_deep_translator.utils import get_translator

from django_deep_translator.utils import get_translator

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -59,13 +57,11 @@ def handle(self, *args, **options):
assert getattr(settings, 'USE_I18N', False), 'i18n framework is disabled'
assert getattr(settings, 'LOCALE_PATHS', []), 'locale paths is not configured properly'
for directory in settings.LOCALE_PATHS:
# walk through all the paths
# and find all the pot files
# walk through all the paths and find all the pot files
for root, dirs, files in os.walk(directory):
for file in files:
if not file.endswith('.po'):
# process file only
# if its a pot file
# process file only if it is a .po file
continue

# get the target language from the parent folder name
Expand All @@ -79,7 +75,7 @@ def handle(self, *args, **options):

def translate_file(self, root, file_name, target_language):
"""
convenience method for translating a pot file
convenience method for translating a po file

:param root: the absolute path of folder where the file is present
:param file_name: name of the file to be translated (it should be a pot file)
Expand All @@ -89,11 +85,14 @@ def translate_file(self, root, file_name, target_language):

po = polib.pofile(os.path.join(root, file_name))
for entry in po:
if not entry.translated():
translation = get_translator()
entry.msgstr = translation.translate_string(text=entry.msgid,source_language=self.source_language, target_language=target_language)
if self.set_fuzzy:
entry.flags.append('fuzzy')
# skip translated
if self.skip_translated and entry.translated():
continue

po.save()
translation = get_translator()
entry.msgstr = translation.translate_string(text=entry.msgid, source_language=self.source_language,
target_language=target_language)
if self.set_fuzzy:
entry.flags.append('fuzzy')

po.save()