def testAll(self): messages = dict(domain1=dict(foo='foo'), domain2=dict(bar='bar')) catalogue = MessageCatalogue('en', messages) self.assertEquals(dict(foo='foo'), catalogue.all('domain1')) self.assertEquals(dict(), catalogue.all('domain88')) self.assertEquals(messages, catalogue.all())
def testHas(self): catalogue = MessageCatalogue( 'en', dict(domain1=dict(foo='foo'), domain2=dict(bar='bar'))) self.assertTrue(catalogue.has('foo', 'domain1')) self.assertFalse(catalogue.has('bar', 'domain1')) self.assertFalse(catalogue.has('foo', 'domain88'))
def testReplace(self): messages = dict(domain1=dict(foo='foo'), domain2=dict(bar='bar')) catalogue = MessageCatalogue('en', messages) messages = dict(foo1='foo1') catalogue.replace(messages, 'domain1') self.assertEquals(messages, catalogue.all('domain1'))
def testget_catalogue(self): translator = Translator('en') self.assertEquals(MessageCatalogue('en'), translator.get_catalogue()) translator.locale = 'fr' self.assertEquals(MessageCatalogue('fr'), translator.get_catalogue('fr'))
def testAddCatalogueWhenLocaleIsNotTheSameAsTheCurrentOne(self): catalogue = MessageCatalogue('en') self.assertRaises( ValueError, lambda: catalogue.add_catalogue( MessageCatalogue( 'fr', dict())))
def testadd_fallback_catalogueWithCircularReference(self): main = MessageCatalogue('en_US') fallback = MessageCatalogue('fr_FR') fallback.add_fallback_catalogue(main) self.assertRaises( ValueError, lambda: main.add_fallback_catalogue(fallback))
def testGetSet(self): catalogue = MessageCatalogue( 'en', dict( domain1=dict( foo='foo'), domain2=dict( bar='bar'))) catalogue.set('foo1', 'foo1', 'domain1') self.assertEquals('foo', catalogue.get('foo', 'domain1')) self.assertEquals('foo1', catalogue.get('foo1', 'domain1'))
def testHas(self): catalogue = MessageCatalogue( 'en', dict( domain1=dict( foo='foo'), domain2=dict( bar='bar'))) self.assertTrue(catalogue.has('foo', 'domain1')) self.assertFalse(catalogue.has('bar', 'domain1')) self.assertFalse(catalogue.has('foo', 'domain88'))
def testDump(self): catalogue = MessageCatalogue('en') catalogue.add({"foo": "bar"}) tmp_dir = tempfile.gettempdir() dumper = PoFileDumper() dumper.dump(catalogue, {"path": tmp_dir}) with open(__DIR__ + '/../fixtures/resources.po') as f1: with open(tmp_dir + '/messages.en.po') as f2: self.assertEqual(f1.read(), f2.read()) os.unlink(tmp_dir + '/messages.en.po')
def testadd_fallback_catalogueWithCircularReference(self): main = MessageCatalogue('en_US') fallback = MessageCatalogue('fr_FR') fallback.add_fallback_catalogue(main) self.assertRaises(ValueError, lambda: main.add_fallback_catalogue(fallback))
def handle(self, *args, **options): if not (bool(options.get('app')) ^ bool(options.get('path'))): print( (bcolors.WARNING + 'You must choose only one of --app or --path' + bcolors.ENDC)) return if not options.get('tranz_dir') and ( not options.get('app') or not settings.TRANZ_SEARCH_LOCALE_IN_APPS): print((bcolors.WARNING + 'You must provide an --tranz-dir when in --path mode, or when TRANZ_SEARCH_LOCALE_IN_APPS ' \ 'settings variable is False.' + bcolors.ENDC)) return self.excluded_paths = [ os.path.abspath(path) for path in options['excluded_paths'] ] self.excluded_paths += [os.path.abspath(django_translate.__path__[0])] self.excluded_paths += settings.TRANZ_EXCLUDED_DIRS self.locale = options['locale'] self.verbosity = options['verbosity'] # Find directories to scan if options.get('app'): for app in list(apps.app_configs.values()): if app.name == options.get('app'): current_name = app.name root_path = app.path break else: raise ValueError("App {0} not found".format( options.get('app'))) else: root_path = os.path.abspath(options['path']) current_name = root_path.split("/")[-1] tranz_dir = options.get('tranz_dir') or os.path.join( root_path, 'tranz') print("Loading existing messages") current_catalogue = MessageCatalogue(options['locale']) loader = services.loader loader.load_messages(tranz_dir, current_catalogue) if len(current_catalogue.messages) == 0: print(("No messages were loaded, make sure there actually are " \ "translation file in format {{catalog}}.{{locale}}.{{format}} in {0}".format(tranz_dir))) return print("Extracting translations") translations = self.extract_translations(services.extractor, root_path) if len(translations) == 0: print(("No messages were extracted, from {0} using {1}".format( root_path, services.extractor.__class__.__name__))) return self.validate_translations(translations, current_catalogue)
def testGetadd_resource(self): catalogue = MessageCatalogue('en') catalogue.add_resource('r') catalogue.add_resource('r') catalogue.add_resource('r1') resources = catalogue.get_resources() resources.sort(key=len) self.assertEquals(['r', 'r1'], resources)
def testGetSet(self): catalogue = MessageCatalogue( 'en', dict(domain1=dict(foo='foo'), domain2=dict(bar='bar'))) catalogue.set('foo1', 'foo1', 'domain1') self.assertEquals('foo', catalogue.get('foo', 'domain1')) self.assertEquals('foo1', catalogue.get('foo1', 'domain1'))
def __init__(self, source, target): """ @type source: MessageCatalogue @type target: MessageCatalogue @raises ValueError """ if source.locale != target.locale: raise ValueError( 'Operated catalogues must belong to the same locale.') self.source = source self.target = target self.result = MessageCatalogue(source.locale) self.domains = None self.messages = {} super(AbstractOperation, self).__init__()
def handle(self, *args, **options): if options.get('force') != True and options.get( 'dump_messages') != True: print( (bcolors.WARNING + 'You must choose at least one of --force or --dump-messages' + bcolors.ENDC)) return if not (bool(options.get('app')) ^ bool(options.get('path'))): print( (bcolors.WARNING + 'You must choose only one of --app or --path' + bcolors.ENDC)) return if not options.get('output_dir') and ( not options.get('app') or not settings.TRANZ_SEARCH_LOCALE_IN_APPS): print((bcolors.WARNING + 'You must provide an --output-dir when in --path mode, or when TRANZ_SEARCH_LOCALE_IN_APPS ' \ 'settings variable is False.' + bcolors.ENDC)) return self.excluded_paths = [ os.path.abspath(path) for path in options['excluded_paths'] ] self.excluded_paths += [os.path.abspath(django_translate.__path__[0])] self.excluded_paths += settings.TRANZ_EXCLUDED_DIRS # Find directories to scan if options.get('app'): for app in list(apps.app_configs.values()): if app.name == options.get('app'): current_name = app.name root_path = app.path break else: raise ValueError("App {0} not found".format( options.get('app'))) else: root_path = os.path.abspath(options['path']) current_name = root_path.split("/")[-1] output_dir = options.get('output_dir') or os.path.join( root_path, 'tranz') writer = services.writer print(('Generating "{0}" translation files for "{1}"'.format( options.get('locale'), current_name))) print("Loading existing messages") current_catalogue = MessageCatalogue(options['locale']) loader = services.loader loader.load_messages(output_dir, current_catalogue) if len(current_catalogue.messages) == 0: print(("No messages were loaded, make sure there actually are " \ "translation file in format {{catalog}}.{{locale}}.{{format}} in {0}".format(output_dir))) return print("Extracting messages") extracted_catalogue = MessageCatalogue(options['locale']) extractor = services.extractor extractor.set_prefix(options['prefix']) self.extract_messages(extractor, root_path, extracted_catalogue) print("Processing catalogues") operation_class = operations.DiffOperation if options[ 'clean'] else operations.MergeOperation operation = operation_class(current_catalogue, extracted_catalogue) if not len(operation.get_domains()): print("No translations found") return if options["dump_messages"]: for domain in operation.get_domains(): print(("Displaying messages for domain {0}".format(domain))) new_keys = list(operation.get_new_messages(domain).keys()) all_keys = list(operation.get_messages(domain).keys()) for id in set(all_keys).difference(new_keys): print(id) for id in new_keys: print((bcolors.OKGREEN + id + bcolors.ENDC)) for id in list(operation.get_obsolete_messages(domain).keys()): print((bcolors.FAIL + id + bcolors.ENDC)) if options["no_backup"]: writer.disable_backup() if options["force"]: print(("Writing files to {0}".format(output_dir))) writer.write_translations(operation.get_result(), options['format'], { "path": output_dir, "default_locale": options['locale'] })
def testGetDomains(self): catalogue = MessageCatalogue('en', {"domain1": {}, "domain2": {}}) self.assertEquals(['domain1', 'domain2'], sorted(catalogue.get_domains()))
def testadd_fallback_catalogue(self): catalogue = MessageCatalogue( 'en_US', dict(domain1=dict(foo='foo'), domain2=dict(bar='bar'))) catalogue.add_resource('r') catalogue1 = MessageCatalogue( 'en', dict(domain1=dict(foo='bar', foo1='foo1'))) catalogue1.add_resource('r1') catalogue.add_fallback_catalogue(catalogue1) self.assertEquals('foo', catalogue.get('foo', 'domain1')) self.assertEquals('foo1', catalogue.get('foo1', 'domain1')) resources = catalogue.get_resources() resources.sort(key=len) self.assertEquals(['r', 'r1'], resources)
def testAdd(self): catalogue = MessageCatalogue( 'en', dict( domain1=dict( foo='foo'), domain2=dict( bar='bar'))) catalogue.add(dict(foo1='foo1'), 'domain1') self.assertEquals('foo', catalogue.get('foo', 'domain1')) self.assertEquals('foo1', catalogue.get('foo1', 'domain1')) catalogue.add(dict(foo='bar'), 'domain1') self.assertEquals('bar', catalogue.get('foo', 'domain1')) self.assertEquals('foo1', catalogue.get('foo1', 'domain1')) catalogue.add(dict(foo='bar'), 'domain88') self.assertEquals('bar', catalogue.get('foo', 'domain88'))
def testAdd(self): catalogue = MessageCatalogue( 'en', dict(domain1=dict(foo='foo'), domain2=dict(bar='bar'))) catalogue.add(dict(foo1='foo1'), 'domain1') self.assertEquals('foo', catalogue.get('foo', 'domain1')) self.assertEquals('foo1', catalogue.get('foo1', 'domain1')) catalogue.add(dict(foo='bar'), 'domain1') self.assertEquals('bar', catalogue.get('foo', 'domain1')) self.assertEquals('foo1', catalogue.get('foo1', 'domain1')) catalogue.add(dict(foo='bar'), 'domain88') self.assertEquals('bar', catalogue.get('foo', 'domain88'))
def testAddCatalogue(self): catalogue = MessageCatalogue( 'en', dict( domain1=dict( foo='foo'), domain2=dict( bar='bar'))) catalogue.add_resource('r') catalogue1 = MessageCatalogue('en', dict(domain1=dict(foo1='foo1'))) catalogue1.add_resource('r1') catalogue.add_catalogue(catalogue1) self.assertEquals('foo', catalogue.get('foo', 'domain1')) self.assertEquals('foo1', catalogue.get('foo1', 'domain1')) resources = catalogue.get_resources() resources.sort(key=len) self.assertEquals(['r', 'r1'], resources)
def testAddCatalogueWhenLocaleIsNotTheSameAsTheCurrentOne(self): catalogue = MessageCatalogue('en') self.assertRaises( ValueError, lambda: catalogue.add_catalogue(MessageCatalogue('fr', dict())))
def testGetLocale(self): catalogue = MessageCatalogue('en') self.assertEquals('en', catalogue.locale)