Skip to content

Commit

Permalink
1.5 - Deprecated unofficial web api based py-googletrans. Use officia…
Browse files Browse the repository at this point in the history
…l python client.
  • Loading branch information
metasmile committed Sep 22, 2018
1 parent e832a51 commit ec2bc1d
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 288 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ While the actual i18n work, my biggest desire was to just quickly fill many empt
pip install strsync
```

#### Enable Google Cloud Translation Python API

Set your account and authentication credentials up with Google's guide for local envirnment.

https://cloud.google.com/translate/docs/reference/libraries#client-libraries-install-python

#### Update Python SSL packages if needed

this is not required for python-2.7.9+
Expand Down
1 change: 0 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ omit =

[run]
include =
googletrans/*
strsync/*

[report]
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
requests==2.13.0
googletrans
requests>=2.13.0
chardet
six>=1.11
fuzzywuzzy
google-cloud-translate
colorama
babel
unicodedata2
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup(
name="strsync",
version="1.4.9",
version="1.5",
packages=[
'strsync',
],
Expand All @@ -30,7 +30,7 @@
'README.rst', encoding='UTF-8'
).read(),
license="MIT",
keywords="translation microsoft strsync strings localizable l10n i18n ios xcode osx mac",
keywords="translation google strsync strings localizable l10n i18n ios xcode osx mac",
url="https://github.com/metasmile/strsync",
include_package_data=True,
classifiers=[
Expand Down
3 changes: 1 addition & 2 deletions strsync/strlocale.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def default_supporting_xcode_lang_codes():
'ru',
'nl',
'pt',
'no',
'nb',
'tr',
'th',
'ro',
Expand All @@ -151,7 +151,6 @@ def default_supporting_xcode_lang_codes():
'zh-Hant',
'sv',
'sk',
'nb',
'ms'
]

Expand Down
7 changes: 3 additions & 4 deletions strsync/strsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def main():
if __INCLUDE_SECONDARY_LANGUAGES__:
__lang_codes += strlocale.secondary_supporting_xcode_lang_codes()

__XCODE_LPROJ_SUPPORTED_LOCALES_MAP__ = strlocale.map_locale_codes(__lang_codes, strtrans.supported_locales())
__XCODE_LPROJ_SUPPORTED_LOCALES_MAP__ = strlocale.map_locale_codes(__lang_codes, strtrans.supported_locale_codes())
__XCODE_LPROJ_SUPPORTED_LOCALES__ = __XCODE_LPROJ_SUPPORTED_LOCALES_MAP__.keys()
print(Fore.WHITE + '(i) Supported numbers of locale code :', str(len(__XCODE_LPROJ_SUPPORTED_LOCALES__)),
Style.RESET_ALL)
Expand Down Expand Up @@ -192,12 +192,12 @@ def insert_or_translate(target_file, lc):
reversed_translated_kv = {}
if len(adding_keys):
print('Translating...')
translated_kv = dict(zip(adding_keys, strtrans.translate([base_kv[k] for k in adding_keys], lc)))
translated_kv = dict(zip(adding_keys, strtrans.translate_strs([base_kv[k] for k in adding_keys], lc)))

if __VERIFY_TRANS_RESULTS__:
print('Reversing results and matching...')
reversed_translated_kv = dict(
zip(adding_keys, strtrans.translate([translated_kv[_ak] for _ak in adding_keys], 'en')))
zip(adding_keys, strtrans.translate_strs([translated_kv[_ak] for _ak in adding_keys], 'en')))

for bk in adding_keys:
if bk in reversed_translated_kv:
Expand Down Expand Up @@ -345,7 +345,6 @@ def resolve_file_names(target_file_names):
continue

base_dict[_file] = parsed_obj
break

if not base_dict:
print('[!] Not found "{0}" in target path "{1}"'.format(__BASE_RESOUCE_DIR__, __RESOURCE_PATH__))
Expand Down
149 changes: 0 additions & 149 deletions strsync/strsync_playground.py

This file was deleted.

18 changes: 10 additions & 8 deletions strsync/strtrans.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import googletrans
from googletrans import Translator
from google.cloud import translate

# Install: https://cloud.google.com/translate/docs/reference/libraries#client-libraries-install-python
# Docs: https://googlecloudplatform.github.io/google-cloud-python/latest/translate/usage.html
import strlocale
import re

Expand Down Expand Up @@ -85,21 +87,21 @@ def __postprocess_str(pretrans_item):
_str = _str.replace(m.replacement, m.literal, 1)
return _str

def supported_locales():
return [l for l in googletrans.LANGCODES.values()]
def supported_locale_codes():
return [l[u'language'] for l in translate.Client().get_languages()]

def translate(strs, to):
__trans = Translator()
def translate_strs(strs, to):
__trans = translate.Client(target_language=to)

assert len(strs) or isinstance(strs[0], str), "Input variables should be string list"
pre_items = [item for item in __preprocessing_translate_strs(strs, to)]

translated_items = __trans.translate([item.trans_input_text for item in pre_items], dest=to)
translated_items = __trans.translate([item.trans_input_text for item in pre_items])
assert len(translated_items) == len(pre_items), "the numbers of input items and translated items must be same."

# map results
for i, t in enumerate(translated_items):
_result = t.text
_result = t[u'translatedText']
_pre_trans_item = pre_items[i]

_literal_replacement_exist = bool(len(_pre_trans_item.matched_literal_items))
Expand Down
2 changes: 1 addition & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pytest
requests==2.13.0
requests>=2.13.0
future==0.14.3
coveralls==1.1
Empty file added tests/__init__.py
Empty file.
6 changes: 0 additions & 6 deletions tests/conftest.py

This file was deleted.

Loading

0 comments on commit ec2bc1d

Please sign in to comment.