import simplejson as enc import gettext def gettext_json(domain, path, lang = [], indent = False): try: tr = gettext.translation(domain, path, lang) # for unknown reasons, instead of having plural entries like # key: [sg, pl1...] # tr._catalog has (key, n): pln, keys = tr._catalog.keys() keys.sort() ret = {} for k in keys: v = tr._catalog[k] if type(k) is tuple: if k[0] not in ret: ret[k[0]] = [] ret[k[0]].append(v) else: ret[k] = v return enc.dumps(ret, ensure_ascii = False, indent = indent) except IOError: return None
#!/usr/bin/python import sys import simplejson as enc import gettext def gettext_json(domain, path, lang = [], indent = False): try: tr = gettext.translation(domain, path, lang) # for unknown reasons, instead of having plural entries like # key: [sg, pl1...] # tr._catalog has (key, n): pln, keys = tr._catalog.keys() keys.sort() ret = {} for k in keys: v = tr._catalog[k] if type(k) is tuple: if k[0] not in ret: ret[k[0]] = [] ret[k[0]].append(v) else: ret[k] = v return enc.dumps(ret, ensure_ascii = True, indent = indent) except IOError as (errno, strerror): print "I/O error({0}): {1}".format(errno, strerror) print gettext_json(sys.argv[1],sys.argv[2],[sys.argv[3]], True)
#!/usr/bin/php -q <?php chdir(__DIR__); $lcPath = './locale'; $jsPath = './static/locale'; foreach (glob($lcPath.'/*/LC_MESSAGES/*.po') as $poFile) { $locale = pathinfo(dirname(dirname($poFile)), PATHINFO_FILENAME); $domain = pathinfo($poFile, PATHINFO_FILENAME); $moFile = dirname($poFile).'/'.$domain.'.mo'; $jsFile = $jsPath.'/'.$locale.'/'.$domain.'.json'; shell_exec('mkdir -p '.escapeshellarg($jsPath.'/'.$locale)); shell_exec('msgfmt -o '.escapeshellarg($moFile).' '.escapeshellarg($poFile)); $cmd = 'gettext2json '.escapeshellarg($domain).' '.escapeshellarg($lcPath ).' '.escapeshellarg($locale).' > '.escapeshellarg($jsFile); shell_exec($cmd); }
<!DOCTYPE html> <html lang="ru"> ... <link href="/locale/ru/mydomain.json" lang="ru" rel="gettext"/> <script type="text/javascript" src="/js/jquery.gettext.js" /> ...
63,66c63,70 < try { < var messages = eval('(' + data + ')'); < } catch(e) { < return; --- > if (typeof(data) == 'object') { > var messages = data; > } else { > try { > var messages = eval('(' + data + ')'); > } catch(e) { > return; > }
Source: https://habr.com/ru/post/108348/
All Articles