def run(self): for name in chain.from_iterable(glob(mask) for mask in LOCALE_MASKS): output = os.path.splitext(name)[0] + ".mo" if not newer(name, output): continue self.announce(f"compiling {name} -> {output}", level=log.INFO) with open(name, "rb") as pofile, open(output, "wb") as mofile: convertmo(pofile, mofile, None)
def view_unit(request, language, domain="django"): localepath = locale_path(language, domain) # XXX in case of POST, use file locking to avoid concurrency issues translation = pofile(file(localepath)) if request.method == "POST": msgid = request.POST["key"] msgstr = request.POST["value"] unit = translation.findunit(msgid) unit.settarget(msgstr) unit.markfuzzy(False) # clear fuzzy flag on save if any # file names t = time.localtime() # XXX no subsecond precision here timestamp = time.strftime("%Y-%m-%dT%H:%M:%S", t) localepath_backup = localepath + "." + timestamp localepath_temp = localepath + ".saved" mopath = localepath.replace(".po", ".mo") mopath_temp = mopath + ".saved" # backup old .po backup = file(localepath_backup, "wb") backup.write(file(localepath, "rb").read()) backup.close() # save .po under temp name translation.savefile(localepath_temp) # overwrite old .po atomically os.rename(localepath_temp, localepath) # compile .mo under temp name _not_empty = convertmo(file(localepath), file(mopath_temp, "wb"), None) # overwrite old mo atomically os.rename(mopath_temp, mopath) # deploy immediately in this Django process purge_gettext_cache() else: msgid = request.GET["key"] unit = translation.findunit(msgid) msgstr = unit.gettarget() return HttpResponse(msgstr, mimetype="text/plain")
] #TODO: add Natural Language classifiers # Compile .mo files from available .po files from translate.tools.pocompile import convertmo mo_files = [] for lang in open(path.join('po', 'LINGUAS')): lang = lang.rstrip() po_filename = path.join('po', lang+'.po') mo_filename = path.join('mo', lang, 'virtaal.mo') if not path.exists(path.join('mo', lang)): os.makedirs(path.join('mo', lang)) convertmo(open(po_filename), open(mo_filename, 'w'), None) mo_files.append( ( path.join(TARGET_DATA_DIR, 'locale', lang, 'LC_MESSAGES'), [mo_filename]) ) # Build lite files as needed on Win32 and OS X if os.name == 'nt' or sys.platform == 'darwin': for lang in open(path.join('po', 'LINGUAS-lite')): app, lang = lang.rstrip().split('/') po_filename = path.join('po', 'lite', app, lang+'.po') mo_filename = path.join('mo', lang, app+'.mo') if not path.exists(path.join('mo', lang)): os.makedirs(path.join('mo', lang))
] #TODO: add Natural Language classifiers # Compile .mo files from available .po files from translate.tools.pocompile import convertmo mo_files = [] for lang in open(path.join('po', 'LINGUAS')): lang = lang.rstrip() po_filename = path.join('po', lang + '.po') mo_filename = path.join('mo', lang, 'virtaal.mo') if not path.exists(path.join('mo', lang)): os.makedirs(path.join('mo', lang)) convertmo(open(po_filename), open(mo_filename, 'w'), None) mo_files.append((path.join(TARGET_DATA_DIR, 'locale', lang, 'LC_MESSAGES'), [mo_filename])) # Build lite files as needed on Win32 and OS X if os.name == 'nt' or sys.platform == 'darwin': for lang in open(path.join('po', 'LINGUAS-lite')): app, lang = lang.rstrip().split('/') po_filename = path.join('po', 'lite', app, lang + '.po') mo_filename = path.join('mo', lang, app + '.mo') if not path.exists(path.join('mo', lang)): os.makedirs(path.join('mo', lang)) convertmo(open(po_filename), open(mo_filename, 'w'), None)
] # TODO: add Natural Language classifiers # Compile .mo files from available .po files from translate.tools.pocompile import convertmo mo_files = [] for po_filename in glob(path.join("po", "*.po")): lang = path.split(po_filename[:-3])[1] # Chop off '.po' mo_filename = path.join("mo", lang, "virtaal.mo") if not path.exists(path.join("mo", lang)): os.makedirs(path.join("mo", lang)) convertmo(open(po_filename), open(mo_filename, "w"), None) mo_files.append((path.join(TARGET_DATA_DIR, "locale", lang, "LC_MESSAGES"), [mo_filename])) # Some of these depend on some files to be built externally before running # setup.py, like the .xml and .desktop files options = { "data_files": [ (path.join(TARGET_DATA_DIR, "virtaal"), glob(path.join(SOURCE_DATA_DIR, "virtaal", "*.*"))), ( path.join(TARGET_DATA_DIR, "virtaal", "autocorr"), glob(path.join(SOURCE_DATA_DIR, "virtaal", "autocorr", "*")), ), (path.join(TARGET_DATA_DIR, "icons"), glob(path.join(SOURCE_DATA_DIR, "icons", "*.*"))), ] + mo_files,