- Currently working on NodeJS, React and a lot of interesting stuff @upkeep with the awesome team of codemymobile.
- Feel free to say hi on LinkedIn or twitter
Need help with your product ? I can help in reaching out to the amazing people @codemymobile.
A simple Django app to automatically translate the pot (.po) files generated by django's makemessages command using google translate.
Home Page: https://ankitpopli1891.github.io/django-autotranslate/
License: MIT License
Need help with your product ? I can help in reaching out to the amazing people @codemymobile.
Add support for translating supported languages via AWS Translate using boto3: http://boto3.readthedocs.io/en/latest/reference/services/translate.html
@ankitpopli1891 If you're open to adding this functionality, I'd love to take a crack at it over the weekend.
from autotranslate.services import translate_strings
res1 = translate_strings(['Hello'], 'de', 'en', False)
res2 = translate_strings(['Hello'], 'fr', 'en', False)
The result of the first call contains one translation into 'de' language as expected.
But the result of the second call contains two translations and the first result element is the translation into 'de' language from the first function call. It seems the function accomulates results between calls.
I expect the result for the second function call has only one translation into 'fr' language.
I am planning to use this app, so do i need Google Translate API Key to make it work??
Got this error due to a 500 error
MacBook-Air-3:jobmind pierreslam$ ./manage.py translate_messages -u -l de
Traceback (most recent call last):
File "./manage.py", line 14, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/autotranslate/management/commands/translate_messages.py", line 69, in handle
self.translate_file(root, file, target_language)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/autotranslate/management/commands/translate_messages.py", line 87, in translate_file
translated_strings = translate_strings(strings, target_language, 'en', False)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/autotranslate/services.py", line 90, in translate_strings
target_language, source_language, optimized)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/autotranslate/services.py", line 90, in translate_strings
target_language, source_language, optimized)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/autotranslate/services.py", line 90, in translate_strings
target_language, source_language, optimized)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/autotranslate/services.py", line 90, in translate_strings
target_language, source_language, optimized)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/autotranslate/services.py", line 90, in translate_strings
target_language, source_language, optimized)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/autotranslate/services.py", line 90, in translate_strings
target_language, source_language, optimized)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/autotranslate/services.py", line 90, in translate_strings
target_language, source_language, optimized)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/autotranslate/services.py", line 88, in translate_strings
self.translate_strings(strings[0:self.max_segments], target_language, source_language, optimized)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/autotranslate/services.py", line 84, in translate_strings
.list(source=source_language, target=target_language, q=strings).execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/oauth2client/util.py", line 132, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googleapiclient/http.py", line 723, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 500 when requesting https://www.googleapis.com/language/translate/v2 returned "Internal Error">
In autotranslate/management/commands/translate_messages.py : 96 hardcoded 'en' as source_language.
Better set to settings.LANGUAGE_CODE or add own setting var for this app
The library is dependent on Google API Python Client which may or may not be used.
Lets keep it optional. It should be installed separately if it is required.
Is this yet another dead GitHub project or author?
The pull requests are not processed, has somebody more information about what is going on?
Collecting django-autotranslate
Using cached django-autotranslate-1.1.0.tar.gz (8.7 kB)
ERROR: Command errored out with exit status 1:
command: /Users/sharat/.virtualenvs/mycroft/bin/python3.7 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/lw/n4rl9vm16t38zzv2x_kl74xc0000gn/T/pip-install-y2vmq72u/django-autotranslate/setup.py'"'"'; __file__='"'"'/private/var/folders/lw/n4rl9vm16t38zzv2x_kl74xc0000gn/T/pip-install-y2vmq72u/django-autotranslate/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/lw/n4rl9vm16t38zzv2x_kl74xc0000gn/T/pip-pip-egg-info-rcg342li
cwd: /private/var/folders/lw/n4rl9vm16t38zzv2x_kl74xc0000gn/T/pip-install-y2vmq72u/django-autotranslate/
Complete output (7 lines):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/lw/n4rl9vm16t38zzv2x_kl74xc0000gn/T/pip-install-y2vmq72u/django-autotranslate/setup.py", line 25, in <module>
requirements = [str(ir.req) for ir in install_requirements]
File "/private/var/folders/lw/n4rl9vm16t38zzv2x_kl74xc0000gn/T/pip-install-y2vmq72u/django-autotranslate/setup.py", line 25, in <listcomp>
requirements = [str(ir.req) for ir in install_requirements]
AttributeError: 'ParsedRequirement' object has no attribute 'req'
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
It is failing when installing via pip 20.1
This issue seems to be related pypa/pip#8188
When converting something like
Hello %(placeholder)s
To Chinese becomes:
您好%(占位符)
The %
symbol becomes a different Unicode character %
.
The (
character also becomes a different Unicode character (
.
Also sometimes the s
disappears or moves.
And so this unfortunately fails at the compilemessages
stage
Change the keyword to continue
instead of return
here or it will not continue parsing other languages than the first one.
One of the dependencies, goslate
, has been abandoned and deleted by its author. It needs to be replaced.
Google has updated its translation service recently with a ticket mechanism to prevent simple crawler program like goslate from accessing. Though a more sophisticated crawler may still work technically, however it would have crossed the fine line between using the service and breaking the service. goslate will not be updated to break google’s ticket mechanism. Free lunch is over. Thanks for using.
I am getting
urllib.error.HTTPError: HTTP Error 503: Service Unavailable
on executing
python manage.py translate_messages
Any idea what's is wrong here?
Hello,
Currently, the library translates for each word within the po files. I think that it should check whether related message id is translated before or not. This helps us to improve the efficiency of using the Google Translate API.
Google Translate API V2 has a limitation per each request should have 128 query entries. I think current version sends without limitation.
I am getting the following exception when I run either a command
python manage.py translate_messages -l 'fr'
or simply
python manage.py translate_messages
` File "/home/john/www/travianpy/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/john/www/travianpy/lib/python3.10/site-packages/django/core/management/base.py", line 448, in execute
output = self.handle(*args, **options)
File "/home/john/www/travianpy/lib/python3.10/site-packages/autotranslate/management/commands/translate_messages.py", line 81, in handle
self.translate_file(root, file, target_language)
File "/home/john/www/travianpy/lib/python3.10/site-packages/autotranslate/management/commands/translate_messages.py", line 101, in translate_file
translated_strings = tl.translate_strings(strings, target_language, self.source_language, False)
File "/home/john/www/travianpy/lib/python3.10/site-packages/autotranslate/services.py", line 44, in translate_strings
assert isinstance(strings, collections.Iterable), 'strings
should a iterable containing string_types'
AttributeError: module 'collections' has no attribute 'Iterable'
`
this is because collections.Iterable is deprecated. Replace it with collections.abc.Iterable.
As a test I modified the code
but I now get this error
`python manage.py translate_messages -l 'fr'
Traceback (most recent call last):
File "/home/john/www/travianpy/manage.py", line 22, in
main()
File "/home/john/www/travianpy/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/john/www/travianpy/lib/python3.10/site-packages/django/core/management/init.py", line 446, in execute_from_command_line
utility.execute()
File "/home/john/www/travianpy/lib/python3.10/site-packages/django/core/management/init.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/john/www/travianpy/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/john/www/travianpy/lib/python3.10/site-packages/django/core/management/base.py", line 448, in execute
output = self.handle(*args, **options)
File "/home/john/www/travianpy/lib/python3.10/site-packages/autotranslate/management/commands/translate_messages.py", line 81, in handle
self.translate_file(root, file, target_language)
File "/home/john/www/travianpy/lib/python3.10/site-packages/autotranslate/management/commands/translate_messages.py", line 101, in translate_file
translated_strings = tl.translate_strings(strings, target_language, self.source_language, False)
File "/home/john/www/travianpy/lib/python3.10/site-packages/autotranslate/services.py", line 48, in translate_strings
return translations if optimized else [_ for _ in translations]
File "/home/john/www/travianpy/lib/python3.10/site-packages/autotranslate/services.py", line 48, in
return translations if optimized else [_ for _ in translations]
File "/home/john/www/travianpy/lib/python3.10/site-packages/goslate.py", line 447, in
return (_unwrapper_single_element(i) for i in
File "/home/john/www/travianpy/lib/python3.10/site-packages/goslate.py", line 221, in _execute
raise exception
File "/usr/lib/python3.10/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/john/www/travianpy/lib/python3.10/site-packages/goslate.py", line 441, in task
r = self._translate_single_text(text, target_language, source_language)
File "/home/john/www/travianpy/lib/python3.10/site-packages/goslate.py", line 340, in _translate_single_text
results = list(self._execute(make_task(i) for i in split_text(text)))
File "/home/john/www/travianpy/lib/python3.10/site-packages/goslate.py", line 209, in _execute
yield each()
File "/home/john/www/travianpy/lib/python3.10/site-packages/goslate.py", line 338, in
return lambda: self._basic_translate(text, target_language, source_lauguage)[0]
File "/home/john/www/travianpy/lib/python3.10/site-packages/goslate.py", line 259, in _basic_translate
data = {'src': raw_data[-1][0][0]}
IndexError: list index out of range
`
Is there an updated version of this or any alternatives available, or am I missing an import somewhere?
I get this error every time I try to used it
$ python3 manage.py translate_messages
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/mestre/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/mestre/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/mestre/.local/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/mestre/.local/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/home/mestre/.local/lib/python3.8/site-packages/autotranslate/management/commands/translate_messages.py", line 76, in handle
self.translate_file(root, file, target_language)
File "/home/mestre/.local/lib/python3.8/site-packages/autotranslate/management/commands/translate_messages.py", line 96, in translate_file
translated_strings = tl.translate_strings(strings, target_language, 'en', False)
File "/home/mestre/.local/lib/python3.8/site-packages/autotranslate/services.py", line 46, in translate_strings
return translations if optimized else [_ for _ in translations]
File "/home/mestre/.local/lib/python3.8/site-packages/autotranslate/services.py", line 46, in <listcomp>
return translations if optimized else [_ for _ in translations]
File "/home/mestre/.local/lib/python3.8/site-packages/goslate.py", line 441, in <genexpr>
return (_unwrapper_single_element(i) for i in
File "/home/mestre/.local/lib/python3.8/site-packages/goslate.py", line 203, in _execute
yield each()
File "/home/mestre/.local/lib/python3.8/site-packages/goslate.py", line 435, in task
r = self._translate_single_text(text, target_language, source_language)
File "/home/mestre/.local/lib/python3.8/site-packages/goslate.py", line 334, in _translate_single_text
results = list(self._execute(make_task(i) for i in split_text(text)))
File "/home/mestre/.local/lib/python3.8/site-packages/goslate.py", line 203, in _execute
yield each()
File "/home/mestre/.local/lib/python3.8/site-packages/goslate.py", line 332, in <lambda>
return lambda: self._basic_translate(text, target_language, source_lauguage)[0]
File "/home/mestre/.local/lib/python3.8/site-packages/goslate.py", line 253, in _basic_translate
data = {'src': raw_data[-1][0][0]}
IndexError: list index out of range
when we type in terminal
python manage.py translate_messages -l de
I believe that is a good feature the possibility of translate from other language diferente of us.
You may create create in settings.py like source_language=xx and default is us.
tks.
Using the translate_messages -l 'fr'
command results in the following error:
urllib.error.HTTPError: HTTP Error 429: Too Many Requests
Using the translate_messages
command without specifying the locale results in the follow error:
File "/usr/local/lib/python3.8/site-packages/goslate.py", line 253, in _basic_translate
data = {'src': raw_data[-1][0][0]}
IndexError: list index out of range
I am getting the following error while running translate_messages
. I have generated a Google Translate API key and set it as GOOGLE_TRANSLATE_KEY
as well.
masked@masked:~/masked$ ./manage.py translate_messages -l ar -u
2021-06-30 08:43:14.585 INFO [autotranslate.management.commands.translate_messages] skipping translation for locale `vi`
2021-06-30 08:43:14.586 INFO [autotranslate.management.commands.translate_messages] filling up translations for locale `ar`
2021-06-30 08:43:14.683 INFO [googleapiclient.discovery_cache] file_cache is only supported with oauth2client<4.0.0
Traceback (most recent call last):
File "./manage.py", line 157, in <module>
execute_from_command_line(sys.argv)
File "./manage.py", line 122, in execute_from_command_line
utility.execute()
File "/srv/masked-py3-venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/srv/masked-py3-venv/lib/python3.6/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/srv/masked-py3-venv/lib/python3.6/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/srv/masked-py3-venv/lib/python3.6/site-packages/autotranslate/management/commands/translate_messages.py", line 81, in handle
self.translate_file(root, file, target_language)
File "/srv/masked-py3-venv/lib/python3.6/site-packages/autotranslate/management/commands/translate_messages.py", line 102, in translate_file
self.update_translations(po, translated_strings)
File "/srv/masked-py3-venv/lib/python3.6/site-packages/autotranslate/management/commands/translate_messages.py", line 154, in update_translations
translation = fix_translation(entry.msgid, translation)
File "/srv/masked-py3-venv/lib/python3.6/site-packages/autotranslate/management/commands/translate_messages.py", line 196, in fix_translation
translation = restore_placeholders(msgid, translation)
File "/srv/masked-py3-venv/lib/python3.6/site-packages/autotranslate/management/commands/translate_messages.py", line 182, in restore_placeholders
translation)
File "/srv/masked-py3-venv/lib/python3.6/re.py", line 191, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "/srv/masked-py3-venv/lib/python3.6/site-packages/autotranslate/management/commands/translate_messages.py", line 181, in <lambda>
lambda matches: '{0}{1}{2}'.format(placehoders[0][0], placehoders[0][1], placehoders.pop(0)[2]),
IndexError: list index out of range
Something like
msgid ""
"Multiline\n"
"Text"
is not supported ATM although very common.
To Fix change add codecs usage to the code in translate_messages.py as the following:
from autotranslate.utils import translate_strings
from django.conf import settings
from django.core.management.base import BaseCommand
from optparse import make_option
import os
import codecs
class Command(BaseCommand):
help = ('autotranslate all the message files that have been generated '
'using the `makemessages` command.')
option_list = BaseCommand.option_list + (
make_option('--locale', '-l', default=[], dest='locale', action='append',
help='autotranslate the message files for the given locale(s) (e.g. pt_BR). '
'can be used multiple times.'),
)
def add_arguments(self, parser):
# Previously, only the standard optparse library was supported and
# you would have to extend the command option_list variable with optparse.make_option().
# See: https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/#accepting-optional-arguments
# In django 1.8, these custom options can be added in the add_arguments()
parser.add_argument('--locale', '-l', default=[], dest='locale', action='append',
help='autotranslate the message files for the given locale(s) (e.g. pt_BR). '
'can be used multiple times.')
def handle(self, *args, **options):
locale = options.get('locale')
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
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
continue
# get the target language from the parent folder name
target_language = os.path.basename(os.path.dirname(root))
if locale and target_language not in locale:
#print "Skipping language %s" % target_language
continue
self.translate_file(root, file, target_language)
@classmethod
def translate_file(cls, root, file_name, target_language):
"""
convenience method for translating a pot 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)
:param target_language: language in which the file needs to be translated
"""
print('translating ', target_language)
strings = []
translations = {}
with codecs.open(os.path.join(root, file_name),'r','utf-8') as _input_file:
original_file = _input_file.readlines()
for index, line in enumerate(original_file):
if line.startswith('msgid'):
# separate the actual string from the whole line
# for each line in input file
strings.append('"'.join(line.split('"')[1:-1]))
if line.startswith('msgstr'):
# map the line number with the raw string
# taken out for translation
translations.update({
index: line
})
# translate the strings,
# all the translated strings are returned
# in the same order on the same index
# viz. [a, b] -> [trans_a, trans_b]
translated_strings = translate_strings(strings, target_language, 'en', False)
# sort the numbers to make sure all the translations
# are injected at the right position
keys = [_ for _ in translations.keys()]
keys.sort()
index = 0
for key in keys:
line = translations.get(key).split('"')
line = [line[0], line[-1]]
line.insert(1, translated_strings[index])
line = '"'.join(line)
translations[key] = line
index += 1
with codecs.open(os.path.join(root, file_name), 'w','utf-8') as output_file:
for index, line in enumerate(original_file):
if index in translations.keys():
line = translations.get(index)
output_file.write(line)
continue
output_file.write(line)
python --version : Python 3.6.8
python manage.py --version : 2.2
output of python manage.py translate_messages
Traceback (most recent call last):
File "manage.py", line 9, in <module>
execute_from_command_line(sys.argv)
File "/home/vagrant/.venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/vagrant/.venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/vagrant/.venv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/vagrant/.venv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/vagrant/.venv/lib/python3.6/site-packages/autotranslate/management/commands/translate_messages.py", line 76, in handle
self.translate_file(root, file, target_language)
File "/home/vagrant/.venv/lib/python3.6/site-packages/autotranslate/management/commands/translate_messages.py", line 96, in translate_file
translated_strings = tl.translate_strings(strings, target_language, 'en', False)
File "/home/vagrant/.venv/lib/python3.6/site-packages/autotranslate/services.py", line 46, in translate_strings
return translations if optimized else [_ for _ in translations]
File "/home/vagrant/.venv/lib/python3.6/site-packages/autotranslate/services.py", line 46, in <listcomp>
return translations if optimized else [_ for _ in translations]
File "/home/vagrant/.venv/lib/python3.6/site-packages/goslate.py", line 441, in <genexpr>
return (_unwrapper_single_element(i) for i in
File "/home/vagrant/.venv/lib/python3.6/site-packages/goslate.py", line 215, in _execute
raise exception
File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/vagrant/.venv/lib/python3.6/site-packages/goslate.py", line 435, in task
r = self._translate_single_text(text, target_language, source_language)
File "/home/vagrant/.venv/lib/python3.6/site-packages/goslate.py", line 334, in _translate_single_text
results = list(self._execute(make_task(i) for i in split_text(text)))
File "/home/vagrant/.venv/lib/python3.6/site-packages/goslate.py", line 203, in _execute
yield each()
File "/home/vagrant/.venv/lib/python3.6/site-packages/goslate.py", line 332, in <lambda>
return lambda: self._basic_translate(text, target_language, source_lauguage)[0]
File "/home/vagrant/.venv/lib/python3.6/site-packages/goslate.py", line 253, in _basic_translate
data = {'src': raw_data[-1][0][0]}
IndexError: list index out of range
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.