def test_test6_unicode_header(self): po_file = open(os.path.join(FOLDER, "test6.po"), "rb") po = Msgfmt(po_file) po.read(header_only=True) po_file.close() self.assertTrue(po.messages[u""].startswith(u"Project-Id-Version: Tøst 1.0")) self.assertEqual(po.encoding, u"utf-8")
def test_test4(self): po_file = open(os.path.join(FOLDER, "test4.po"), "rb") po = Msgfmt(po_file) po.read(header_only=True) po_file.close() self.assertTrue(po.messages[u""].startswith("Project-Id-Version: foo")) self.assertEqual(po.encoding, u"iso-8859-1")
def test_test4(self): po_file = open(os.path.join(FOLDER, 'test4.po'), 'rb') po = Msgfmt(po_file) po.read(header_only=True) po_file.close() self.assertTrue( po.messages[u('')].startswith('Project-Id-Version: foo')) self.assertEqual(po.encoding, u('iso-8859-1'))
def test_test6_unicode_header(self): po_file = open(os.path.join(FOLDER, 'test6.po'), 'rb') po = Msgfmt(po_file) po.read(header_only=True) po_file.close() self.assertTrue(po.messages[u('')].startswith( u('Project-Id-Version: Tøst 1.0', 'utf-8'))) self.assertEqual(po.encoding, u('utf-8'))
def test_test5_unicode_name(self): po_file = open(os.path.join(FOLDER, "test5.po"), "rb") po = Msgfmt(po_file, name=u"dømain") try: with self.assertRaises(PoSyntaxError): po.read() finally: po_file.close() self.assertEqual(po.encoding, u"utf-8")
def test_test5_unicode_name(self): po_file = open(os.path.join(FOLDER, 'test5.po'), 'rb') po = Msgfmt(po_file, name=u('dømain', 'utf-8')) try: with self.assertRaises(PoSyntaxError): po.read() finally: po_file.close() self.assertEqual(po.encoding, u('utf-8'))
def test_test5(self): po_file = open(os.path.join(FOLDER, 'test5.po'), 'rb') po = Msgfmt(po_file) try: with self.assertRaises(PoSyntaxError): po.read() finally: po_file.close() self.assertEqual(po.encoding, u'utf-8')
def test_escape(self): po_file = open(os.path.join(FOLDER, "test_escape.po"), "rb") po = Msgfmt(po_file) try: with self.assertRaises(PoSyntaxError) as e: po.read() self.assertTrue("line 19" in e.exception.msg) self.assertEqual(po.encoding, u"utf-8") finally: po_file.close()
def test_plural(self): po_file = open(os.path.join(FOLDER, "test_plural.po"), "rb") po = Msgfmt(po_file) try: po.read() finally: po_file.close() self.assertEqual( set(po.messages.keys()), set([u"", u"m1", u"m2 ø\x00{d} ømsgid", u"øcontext\x04m3 ø\x00{d} ømsgid context"]) )
def test_escape(self): po_file = open(os.path.join(FOLDER, 'test_escape.po'), 'rb') po = Msgfmt(po_file) try: with self.assertRaises(PoSyntaxError) as e: po.read() self.assertTrue('line 19' in e.exception.msg) self.assertEqual(po.encoding, u('utf-8')) finally: po_file.close()
def test_plural(self): po_file = open(os.path.join(FOLDER, 'test_plural.po'), 'rb') po = Msgfmt(po_file) try: po.read() finally: po_file.close() self.assertEqual( set(po.messages.keys()), set([u'', u'm1', u'm2 ø\x00{d} ømsgid', u'øcontext\x04m3 ø\x00{d} ømsgid context']))
def compile_mo_file(domain, lc_messages_path): """Creates or updates a mo file in the locales folder.""" if not HAS_PYTHON_GETTEXT: logger.critical( "Unable to compile messages: Python `gettext` library missing.") return base = join(lc_messages_path, domain) pofile = str(base + '.po') mofile = str(base + '.mo') po_mtime = 0 try: po_mtime = os.stat(pofile)[ST_MTIME] except (IOError, OSError): return mo_mtime = 0 if os.path.exists(mofile): # Update mo file? try: mo_mtime = os.stat(mofile)[ST_MTIME] except (IOError, OSError): return if po_mtime > mo_mtime: try: mo = Msgfmt(pofile, domain).getAsFile() fd = open(mofile, 'wb') fd.write(mo.read()) fd.close() except (IOError, OSError, PoSyntaxError): logger.warn('Error while compiling %s' % pofile)
def compile_mo_file(domain, lc_messages_path): """Creates or updates a mo file in the locales folder.""" if not HAS_PYTHON_GETTEXT: logger.warn("Unable to compile messages: Python `gettext` library missing.") return base = join(lc_messages_path, domain) pofile = str(base + '.po') mofile = str(base + '.mo') po_mtime = 0 try: po_mtime = os.stat(pofile)[ST_MTIME] except (IOError, OSError): return mo_mtime = 0 if os.path.exists(mofile): # Update mo file? try: mo_mtime = os.stat(mofile)[ST_MTIME] except (IOError, OSError): return if po_mtime > mo_mtime: try: mo = Msgfmt(pofile, domain).getAsFile() fd = open(mofile, 'wb') fd.write(mo.read()) fd.close() except (IOError, OSError, PoSyntaxError): logger.warn('Error while compiling %s' % pofile)
def _load_i18n_dir(basepath): """ Loads an i18n directory (Zope3 PTS format) Format: Products/MyProduct/i18n/*.po The language and domain are stored in the po file """ # load po files basepath = os.path.normpath(basepath) log('Looking for po files in ' + basepath, logging.DEBUG) names = fnmatch.filter(os.listdir(basepath), '*.po') if not names: log('Nothing found in ' + basepath, logging.DEBUG) return registered = [] for name in names: lang = None domain = None pofile = join(basepath, name) po = Msgfmt(pofile, None) po.read(header_only=True) header = po.messages.get('', None) if header is not None: mime_header = {} pairs = [l.split(':', 1) for l in header.split('\n') if l] for key, value in pairs: mime_header[key.strip().lower()] = value.strip() lang = mime_header.get('language-code', None) domain = mime_header.get('domain', None) if lang is not None and domain is not None: if _checkLanguage(lang): reg = (name, basepath, lang, domain, True) _register_catalog_file(*reg) registered.append(name) log('Initialized:', detail = str(len(registered)) + (' message catalogs in %s\n' % basepath))
def _updateMoFile(name, msgpath, lang, domain, mofile): """ Creates or updates a mo file in the locales folder. Returns True if a new file was created. """ pofile = join(msgpath, name) create = False update = False try: po_mtime = os.stat(pofile)[ST_MTIME] except (IOError, OSError): po_mtime = 0 if os.path.exists(mofile): # Update mo file? try: mo_mtime = os.stat(mofile)[ST_MTIME] except (IOError, OSError): mo_mtime = 0 if po_mtime > mo_mtime: # Update mo file update = True else: # Mo file is current return else: # Create mo file create = True if create or update: try: mo = Msgfmt(pofile, domain).getAsFile() fd = open(mofile, 'wb') fd.write(mo.read()) fd.close() except (IOError, OSError, PoSyntaxError): log('Error while compiling %s' % pofile, logging.WARNING) return if create: return True return None
def compile_mo_file(podir, pofile): domain = pofile[:-3] mofile = os.path.join(podir, domain + '.mo') pofile = os.path.join(podir, pofile) # check timestamps: try: do_compile = os.stat(mofile).st_mtime < os.stat(pofile).st_mtime except OSError: do_compile = True if do_compile: self.logger.debug('Compiling po-file: %s' % pofile) try: mo = Msgfmt(pofile, name=domain).getAsFile() fd = open(mofile, 'wb') fd.write(mo.read()) fd.close() except (IOError, OSError, PoSyntaxError): self.logger.warn('Error while compiling %s' % pofile)
def compile(conf, domain, lang): path = os.path.join(os.environ.get('INSTANCE_HOME'), 'var/amberjack_i18n') path = os.path.normpath(path) if not os.path.isdir(path): os.makedirs(path) filename = '%s-%s' % (lang, os.path.basename(conf.name)) po = open(os.path.join(path, filename), 'w') po.write(conf.read()) po.close() po = open(po.name, 'r') _mo = Msgfmt(po, domain).getAsFile() mo = open(os.path.join(path, filename.replace('.po','.mo')), 'wb') mo.write(_mo.read()) mo.close() _mo.close() return str(mo.name)
def compile_mo_files(input_dir=I18N_PATH, output_dir=MO_FILES_BASE): """ Compile all cc-style po files to mo files. Keyword arguments: - input_dir: Directory of input files to compile - output_dir: Directory where we'll put compiled MO files """ for catalog in os.listdir(input_dir): catalog_path = os.path.join(input_dir, catalog) po_path = os.path.join(catalog_path, 'cc_org.po') if not os.path.isdir(catalog_path) or not os.path.exists(po_path): continue po_mtime = os.stat(po_path)[ST_MTIME] if not os.path.exists(output_dir): os.mkdir(output_dir) if not os.path.exists(os.path.join(output_dir, catalog)): os.mkdir(os.path.join(output_dir, catalog)) if not os.path.exists(os.path.join( output_dir, catalog, 'LC_MESSAGES')): os.mkdir(os.path.join( output_dir, catalog, 'LC_MESSAGES')) mo_path = os.path.join( output_dir, catalog, 'LC_MESSAGES', 'cc_org.mo') # don't compile mo files when we don't need to. if os.path.exists(mo_path): mo_mtime = os.stat(mo_path)[ST_MTIME] if po_mtime == mo_mtime: continue mo_data = Msgfmt(po_path, 'cc_org').getAsFile() fd = open(mo_path, 'wb') fd.write(mo_data.read()) fd.close()
def compile_mo_file(podir, pofile): domain = pofile[:-3] mofile = os.path.join(podir, domain + '.mo') pofile = os.path.join(podir, pofile) # check timestamps: try: do_compile = os.stat(mofile).st_mtime < os.stat(pofile).st_mtime except OSError: do_compile = True if do_compile: self.logger.debug('Compiling po-file: %s' % pofile) try: mo = Msgfmt(pofile, name=domain).getAsFile() fd = open(mofile, 'wb') fd.write(mo.read()) fd.close() except (IOError, OSError, PoSyntaxError): msg = "Error while compiling language file %s" % mofile if self._quiet: self.logger.debug(msg) else: self.logger.error(msg)
def compile_mo_files(input_dir=I18N_PATH, output_dir=MO_FILES_BASE): """ Compile all cc-style po files to mo files. Keyword arguments: - input_dir: Directory of input files to compile - output_dir: Directory where we'll put compiled MO files """ for catalog in os.listdir(input_dir): catalog_path = os.path.join(input_dir, catalog) po_path = os.path.join(catalog_path, 'cc_org.po') if not os.path.isdir(catalog_path) or not os.path.exists(po_path): continue po_mtime = os.stat(po_path)[ST_MTIME] if not os.path.exists(output_dir): os.mkdir(output_dir) if not os.path.exists(os.path.join(output_dir, catalog)): os.mkdir(os.path.join(output_dir, catalog)) if not os.path.exists(os.path.join(output_dir, catalog, 'LC_MESSAGES')): os.mkdir(os.path.join(output_dir, catalog, 'LC_MESSAGES')) mo_path = os.path.join(output_dir, catalog, 'LC_MESSAGES', 'cc_org.mo') # don't compile mo files when we don't need to. if os.path.exists(mo_path): mo_mtime = os.stat(mo_path)[ST_MTIME] if po_mtime == mo_mtime: continue mo_data = Msgfmt(po_path, 'cc_org').getAsFile() fd = open(mo_path, 'wb') fd.write(mo_data.read()) fd.close()
def compile_mo_file(podir, pofile): domain = pofile[:-3] mofile = os.path.join(podir, domain + ".mo") pofile = os.path.join(podir, pofile) # check timestamps: try: do_compile = os.stat(mofile).st_mtime < os.stat( pofile).st_mtime except OSError: do_compile = True if do_compile: self.logger.debug("Compiling po-file: %s" % pofile) try: mo = Msgfmt(pofile, name=domain).getAsFile() fd = open(mofile, "wb") fd.write(mo.read()) fd.close() except (IOError, OSError, PoSyntaxError): msg = "Error while compiling language file %s" % mofile if self._quiet: self.logger.debug(msg) else: self.logger.error(msg)
def test_escape(self): po_file = file(os.path.join(FOLDER, 'test_escape.po'), 'rb') po = Msgfmt(po_file) with self.assertRaises(PoSyntaxError) as e: po.read() self.assertTrue('line 19' in e.exception.msg)
def test_test4(self): po_file = file(os.path.join(self.folder, 'test4.po'), 'rb') po = Msgfmt(po_file) po.read(header_only=True) self.failUnless(po.messages[''].startswith('Project-Id-Version: foo'))
def test_test5_unicode_name(self): po_file = file(os.path.join(FOLDER, 'test5.po'), 'rb') po = Msgfmt(po_file, name=u'dømain') with self.assertRaises(PoSyntaxError): po.read()
def test_test5(self): po_file = file(os.path.join(FOLDER, 'test5.po'), 'rb') po = Msgfmt(po_file) with self.assertRaises(PoSyntaxError): po.read()
def test_test4(self): po_file = file(os.path.join(FOLDER, 'test4.po'), 'rb') po = Msgfmt(po_file) po.read(header_only=True) self.assertTrue(po.messages[''].startswith('Project-Id-Version: foo'))