Skip to content

Commit

Permalink
store canonical strings in static-files/strings.json and autogenerate…
Browse files Browse the repository at this point in the history
… our traditional JS locale files in src/locale. All unit tests pass again, and dialogs should work fine too.
  • Loading branch information
toolness committed Nov 12, 2011
1 parent 4d822e1 commit f6c1243
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ browser-addon/chrome/webxray.js
src/settings.local.js
static-files/mix-master-dialog
/locale
/src/locale/*.json
~/src/locale/en.json
/src/locale
2 changes: 1 addition & 1 deletion babel.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[extractors]
webxray = localization:webxray_extract

[webxray: en.json]
[webxray: strings.json]
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
, "jquery/src/dimensions.js"
, "src/get-bookmarklet-url.js"
, "src/localization.js"
, "src/locale/*.json"
, "src/locale/*.js"
, "src/settings.js"
, "src/help.js"
, "src/settings.local.js"
Expand Down
21 changes: 4 additions & 17 deletions go.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,6 @@ def get_git_commit():
except Exception:
return "unknown"

def process_locale_json(data):
lines = []
for locale in data:
for scope in data[locale]:
lines.append("jQuery.localization.extend(%s, %s, %s);" % (
json.dumps(locale),
json.dumps(scope),
json.dumps(data[locale][scope])
))
return '\n'.join(lines)

def build_compiled_file(cfg):
metadata = json.dumps(dict(commit=get_git_commit(),
date=time.ctime()))
Expand All @@ -69,10 +58,7 @@ def build_compiled_file(cfg):
filenames = [path]
for filename in filenames:
data = open(filename, 'r').read()
if filename.endswith('.json'):
data = process_locale_json(json.loads(data))
else:
data = data.replace('__BUILD_METADATA__', metadata)
data = data.replace('__BUILD_METADATA__', metadata)
contents.append(data)
return ''.join(contents)

Expand Down Expand Up @@ -130,15 +116,16 @@ def serve(cfg, ip=''):
elif cmd == 'globalserve':
serve(cfg)
elif cmd == 'compilemessages':
localization.compilemessages(json_dir=path('src', 'locale'),
localization.compilemessages(json_dir=path(cfg['staticFilesDir']),
js_locale_dir=path('src', 'locale'),
locale_dir=locale_dir,
locale_domain=locale_domain)
elif cmd == 'makemessages':
locale = None
if len(sys.argv) > 2:
locale = sys.argv[2]
localization.makemessages(babel_ini_file=path('babel.ini'),
json_dir=path('src', 'locale'),
json_dir=path(cfg['staticFilesDir']),
locale_dir=locale_dir,
locale_domain=locale_domain,
locale=locale)
Expand Down
File renamed without changes.
36 changes: 25 additions & 11 deletions vendor/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
except ImportError:
import simplejson as json

def process_locale_json(data):
lines = []
for locale in data:
for scope in data[locale]:
lines.append("jQuery.localization.extend(%s, %s, %s);" % (
json.dumps(locale),
json.dumps(scope),
json.dumps(data[locale][scope])
))
return '\n'.join(lines)

def webxray_extract(fileobj, keywords, comment_tags, options):
data = json.load(fileobj)
for locale in data:
Expand All @@ -30,35 +41,38 @@ def locale_exists(locale, dirname, domain):
'%s.po' % domain)
return os.path.exists(pofile)

def compilemessages(json_dir, locale_dir, locale_domain):
"convert message files into binary and JSON formats"
def compilemessages(json_dir, js_locale_dir, locale_dir, locale_domain):
"convert message files into binary and JS formats"

data = json.load(open(os.path.join(json_dir, 'en.json')))['en']
data = json.load(open(os.path.join(json_dir, 'strings.json')))['en']
babel(['compile', '--use-fuzzy', '-d', locale_dir, '-D',
locale_domain])
locales = find_locales(locale_dir, locale_domain)
locales = find_locales(locale_dir, locale_domain) + ['en']
for locale in locales:
nice_locale = locale.replace('_', '-')
print "processing localization '%s'" % nice_locale
trans = gettext.translation(locale_domain,
locale_dir,
[locale])
if locale == 'en':
trans = gettext.NullTranslations()
else:
trans = gettext.translation(locale_domain,
locale_dir,
[locale])
newtrans = {}
newtrans[locale] = {}
for scope in data:
scopedict = {}
for key in data[scope]:
original = data[scope][key]
translation = trans.ugettext(original)
if translation != original:
if translation != original or locale == 'en':
scopedict[key] = translation
if scopedict:
newtrans[locale][scope] = scopedict
if newtrans[locale]:
basename = "%s.json" % nice_locale
basename = "%s.js" % nice_locale
print " writing %s" % basename
out = open(os.path.join(json_dir, basename), 'w')
out.write(json.dumps(newtrans))
out = open(os.path.join(js_locale_dir, basename), 'w')
out.write(process_locale_json(newtrans))
out.close()

def makemessages(babel_ini_file, json_dir, locale_dir,
Expand Down

0 comments on commit f6c1243

Please sign in to comment.