Combine i18n.py with translation.py

This commit is contained in:
Keith Mitchell
2011-12-09 16:40:25 -08:00
parent 00f97a47ff
commit 9e1b32db14
3 changed files with 28 additions and 58 deletions

View File

@@ -1,37 +0,0 @@
# The contents of this file are subject to the Common Public Attribution
# License Version 1.0. (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
# License Version 1.1, but Sections 14 and 15 have been added to cover use of
# software over a computer network and provide for limited attribution for the
# Original Developer. In addition, Exhibit A has been modified to be consistent
# with Exhibit B.
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
#
# The Original Code is Reddit.
#
# The Original Developer is the Initial Developer. The Initial Developer of the
# Original Code is CondeNet, Inc.
#
# All portions of the code written by CondeNet are Copyright (c) 2006-2010
# CondeNet, Inc. All Rights Reserved.
################################################################################
import os
import glob
try:
import reddit_i18n
I18N_PATH = os.path.dirname(reddit_i18n.__file__)
except ImportError:
I18N_PATH = os.path.abspath('r2/r2/i18n')
def get_available_languages():
search_expression = os.path.join(I18N_PATH, "*", "LC_MESSAGES", "r2.mo")
mo_files = glob.glob(search_expression) # _i18n_path/<lang code>/LC_MESSAGES/r2.mo
languages = [os.path.basename(os.path.dirname(os.path.dirname(p))) for p in mo_files]
return sorted(languages)

View File

@@ -5,7 +5,7 @@ from subprocess import Popen, PIPE
import re
import json
from r2.lib.i18n import get_available_languages
from r2.lib.translation import iter_langs
if __name__ != "__main__":
from pylons import g, c
@@ -176,7 +176,7 @@ class LocalizedModule(Module):
string_keys = re.findall("r\.strings\.([\w$_]+)", reddit_source)
print >> sys.stderr, "Creating language-specific files:"
for lang in get_available_languages():
for lang, unused in iter_langs():
strings = StringsSource(lang, string_keys)
source = strings.get_source()
lang_path = LocalizedModule.languagize_path(self.path, lang)
@@ -202,8 +202,7 @@ class LocalizedModule(Module):
@property
def outputs(self):
languages = get_available_languages()
for lang in languages:
for lang, unused in iter_langs():
yield LocalizedModule.languagize_path(self.path, lang)
class JQuery(Module):

View File

@@ -24,7 +24,11 @@ import os
import pylons
from pylons.i18n.translation import translation, LanguageError, NullTranslations
from r2.lib.i18n import I18N_PATH as _i18n_path
try:
import reddit_i18n
I18N_PATH = os.path.dirname(reddit_i18n.__file__)
except ImportError:
I18N_PATH = os.path.abspath('r2/r2/i18n')
_domain = 'r2'
@@ -36,7 +40,7 @@ def _get_translator(lang, graceful_fail=False, **kwargs):
if not isinstance(lang, list):
lang = [lang]
try:
translator = translation(conf['pylons.package'], _i18n_path,
translator = translation(conf['pylons.package'], I18N_PATH,
languages=lang, **kwargs)
except IOError, ioe:
if graceful_fail:
@@ -57,28 +61,32 @@ def set_lang(lang, graceful_fail = False, **kwargs):
registry.replace(pylons.translator, translator)
def load_data(lang, path=_i18n_path, domain=_domain, extension='data'):
filename = os.path.join(path, lang, 'LC_MESSAGES',
domain + '.' + extension)
def load_data(lang_path, domain=_domain, extension='data'):
filename = os.path.join(lang_path, domain + '.' + extension)
with open(filename) as datafile:
data = json.load(datafile)
return data
def get_active_langs(path=_i18n_path, default_lang='en'):
def iter_langs(base_path=I18N_PATH):
for lang in os.listdir(base_path):
full_path = os.path.join(base_path, lang, 'LC_MESSAGES')
if os.path.isdir(full_path):
yield lang, full_path
def get_active_langs(path=I18N_PATH, default_lang='en'):
trans = []
trans_name = {}
for lang in os.listdir(path):
x = os.path.join(path, lang, 'LC_MESSAGES')
if os.path.isdir(x):
data = load_data(lang)
name = [data['name'], '']
if data['_is_enabled'] and lang != default_lang:
trans.append(lang)
completion = float(data['num_completed']) / float(data['num_total'])
if completion < .5:
name[1] = ' (*)'
trans_name[lang] = name
for lang, lang_path in iter_langs(path):
data = load_data(lang_path)
name = [data['name'], '']
if data['_is_enabled'] and lang != default_lang:
trans.append(lang)
completion = float(data['num_completed']) / float(data['num_total'])
if completion < .5:
name[1] = ' (*)'
trans_name[lang] = name
trans.sort()
# insert the default language at the top of the list
trans.insert(0, default_lang)