def text_for_language_code(self, lang_code): new_code = find_closest_supported_language(lang_code) if new_code not in self.translations_by_language_code.keys(): # we don't have a translation for this language return self.text translation_id = self.translations_by_language_code[new_code] translation = Translation.objects.get(id=translation_id) return translation.text
def test_find_closest_supported_language(self): """ Test the `find_closest_supported_language` function. """ # If the language code matches exactly, then it should just be returned with override_settings(LANGUAGES=[('en', 'English')]): self.assertEqual(find_closest_supported_language("en"), "en") # Same exact matching logic applies to 2-part language codes with override_settings(LANGUAGES=[('en-us', 'English')]): self.assertEqual(find_closest_supported_language("en-us"), "en-us") # If the language code is in 2 parts and the first half matches one of the supported # languages then it should return that supported language with override_settings(LANGUAGES=[('en', 'English')]): self.assertEqual(find_closest_supported_language("en-us"), "en") # If the language code matches the first half of one of the supported languages then it # should return that supported language with override_settings(LANGUAGES=[('en-us', 'English')]): self.assertEqual(find_closest_supported_language("en"), "en-us") # If there is no sensible match then it should raise ValueError with override_settings(LANGUAGES=[('fr', 'Francais')]): self.assertRaises(ValueError, find_closest_supported_language, "en")
def handle(self, *args, **options): if len(args) != 0: raise CommandError("Command doesn't accept any arguments") locale = getattr(settings, "LANGUAGE_CODE", "en") locale = find_closest_supported_language(locale) verbosity = int(options.get('verbosity')) extensions = options.get('extensions') symlinks = options.get('symlinks') ignore_patterns = options.get('ignore_patterns') if options.get('use_default_ignore_patterns'): ignore_patterns += ['CVS', '.*', '*~'] # ignore contents of test directories, common libraries and source control ignore_patterns += ['*/tests/*', '*tests.py'] ignore_patterns += ['django/*', 'djangoappengine/*', 'djangotoolbox/*', 'mapreduce/*'] ignore_patterns += ['.svn/*', '.git/*'] ignore_patterns += ['*.pyc', '*.pyo', '*.pyd'] if hasattr(settings, 'FLUENT_EXTRA_IGNORE_PATTERNS'): ignore_patterns += settings.FLUENT_EXTRA_IGNORE_PATTERNS ignore_patterns = list(set(ignore_patterns)) if verbosity > 1: sys.stdout.write('examining files with the extensions: %s\n' % get_text_list(list(extensions), 'and')) trans = make_messages(locale, verbosity, extensions, symlinks, ignore_patterns) if options.get("group"): group = options.get("group") trans = [ x for x in trans if group in x["fields"]['used_by_groups_in_templates'] ] if options.get('pot_filename'): write_pot_file(options['pot_filename'], trans) if options.get('csv_filename'): write_csv_file(options['csv_filename'], trans)