def test_switch_locale(self): bundle = Bundle(['tests/main.ftl'], default_locale='en') self.assertEqual(bundle.format('simple'), "Simple") activate('tr') self.assertEqual(bundle.format('simple'), "Basit") deactivate() self.assertEqual(bundle.format('simple'), "Simple")
def test_logged_runtime_errors(self): bundle = Bundle(['tests/main.ftl'], default_locale='en') def run(locale_expected): with LogCapture() as log: self.assertEqual(bundle.format('with-argument', {}), 'Hello to \u2068user\u2069.') this_file = os.path.abspath(__file__) ftl_filename = os.path.normpath(os.path.join( this_file, '..', 'locales', 'en', 'tests', 'main.ftl' )) log.check_present( ( 'django_ftl.message_errors', 'ERROR', "FTL exception for locale [%s], message 'with-argument', args {}: FluentReferenceError(%s'%s:5:28: Unknown external: user'%s)" % ( locale_expected, 'u' if six.PY2 else '', ftl_filename, ',' if sys.version_info < (3, 7) else '' ) ) ) # Run multiple times, because second time has a different path due to caching run('None') run('None') activate('en') run('en') run('en')
def test_locale_matching_case_insensitive(self): activate('fr-fr') bundle = Bundle(['tests/main.ftl'], default_locale='en') self.assertEqual(bundle.format('simple'), 'Facile') activate('EN') bundle = Bundle(['tests/main.ftl'], default_locale='en') self.assertEqual(bundle.format('simple'), 'Simple')
def test_allow_module_level_format_lazy(self): import tests.allow_module_level_format_lazy self.assertRaises(NoLocaleSet, text_type, tests.allow_module_level_format_lazy.MyThing.my_label) activate('fr-FR') self.assertEqual(force_text(tests.allow_module_level_format_lazy.MyThing.my_label), 'Facile')
def test_require_activate_after_activate(self): bundle = Bundle(['tests/main.ftl'], default_locale='en', require_activate=True) activate('en') self.assertEqual(bundle.format('simple'), 'Simple') deactivate() self.assertRaises(NoLocaleSet, bundle.format, 'simple')
def thread_1(): with lock: activate('en') output.append((1, bundle.format('simple'))) wait_until_output(2) with lock: # Should still be in English, output.append((1, bundle.format('simple')))
def test_usage_docs(self): # These tests parallel the code in the usage.rst docs activate('en') title = ftl_bundle.format('events-title') self.assertEqual(title, 'MyApp Events!') greeting = ftl_bundle.format('events-greeting', {'username': '******'}) self.assertEqual(greeting, 'Hello, \u2068boaty mcboatface\u2069')
def test_lazy(self): bundle = Bundle(['tests/main.ftl'], default_locale='en') l = bundle.format_lazy('simple') self.assertEqual(force_text(l), 'Simple') activate('fr-FR') self.assertEqual(force_text(l), 'Facile') deactivate() self.assertEqual(force_text(l), 'Simple')
def test_locale_range_lookup_missing(self): # There is no fr or fr-XY (only fr-FR), neither of these should fallback # to fr-FR activate('fr') bundle = Bundle(['tests/main.ftl'], default_locale='en') self.assertEqual(bundle.format('simple'), 'Simple') activate('fr-XY') bundle = Bundle(['tests/main.ftl']) self.assertEqual(bundle.format('simple'), 'Simple')
def test_locale_range_lookup_list(self): # fr-XY doesn't exist, fr-FR does activate('fr-XY, fr-FR') bundle = Bundle(['tests/main.ftl'], default_locale='en') self.assertEqual(bundle.format('simple'), 'Facile') # en-GB doesn't exist, en does activate('en-GB, en') bundle = Bundle(['tests/main.ftl'], default_locale='en') self.assertEqual(bundle.format('simple'), 'Simple')
def test_number_formatting(self): bundle = Bundle(['tests/main.ftl'], default_locale='en') self.assertEqual(bundle.format('with-number-argument', {'points': 1234567}), 'Score: \u20681,234,567\u2069') activate('fr-FR') self.assertEqual(bundle.format('with-number-argument', {'points': 1234567}), 'Points: \u20681\u202f234\u202f567\u2069') deactivate() self.assertEqual(bundle.format('with-number-argument', {'points': 1234567}), 'Score: \u20681,234,567\u2069')
def test_number_formatting_for_fallback(self): # When we fall back to a default, number formatting # should be consistent with the language. # German locale: 1.234.567 # System locale (would probably be 'en',): 1,234,567 # French locale: 1 234 567 bundle = Bundle(['tests/main.ftl'], default_locale='fr-FR') activate('de') # Should get French words and formatting self.assertEqual(bundle.format('with-number-argument', {'points': 1234567}), 'Points: \u20681\u202f234\u202f567\u2069')
def thread_2(): # Make sure thread_1 goes first: wait_until_output(1) with lock: activate('fr-FR') output.append((2, bundle.format('simple'))) wait_until_output(3) with lock: # Should still be French output.append((2, bundle.format('simple'))) activate('en') # Should allow switching output.append((2, bundle.format('simple')))
def test_lazy_with_require_activate(self): bundle = Bundle(['tests/main.ftl'], default_locale='en', require_activate=True) self.assertRaises(NoLocaleSet, bundle.format, 'simple') msg = bundle.format_lazy('simple') self.assertRaises(NoLocaleSet, force_text, msg) activate('en') self.assertEqual(force_text(msg), 'Simple') activate('fr-FR') self.assertEqual(force_text(msg), 'Facile')
def test_simple_string_default_locale_present(bundle, benchmark): activate('en') result = benchmark(lambda: bundle.format('simple-string')) assert result == "Hello I am a simple string"
def test_simple_string_other_locale_present(bundle, benchmark): activate('tr') result = benchmark(lambda: bundle.format('simple-string')) assert result == "Merhaba ben basit bir metinim"
def test_simple_string_present_in_fallback(bundle, benchmark): activate('tr') result = benchmark( lambda: bundle.format('simple-string-present-in-fallback')) assert result == "Hello I am a simple string present in fallback"
def test_missing(self): activate('en') bundle = Bundle(['tests/main.ftl'], default_locale='en') self.assertEqual(bundle.format('missing-from-all'), "???")
def test_handle_underscores_in_locale_name(self): activate('fr_FR') bundle = Bundle(['tests/main.ftl'], default_locale='en') self.assertEqual(bundle.format('simple'), 'Facile')
def test_locale_matching_for_default_locale(self): activate('zh') bundle = Bundle(['tests/main.ftl'], default_locale='EN') # 'EN' not 'en' self.assertEqual(bundle.format('simple'), 'Simple')
def test_locale_range_lookup(self): # en-US does not exist, but 'en' does and should be found activate('en-US') bundle = Bundle(['tests/main.ftl'], default_locale='en') self.assertEqual(bundle.format('simple'), 'Simple')
def test_find_messages(self): bundle = Bundle(['tests/main.ftl']) activate('en') self.assertEqual(bundle.format('simple'), "Simple")
def test_load_multiple_with_some_missing(self): bundle = Bundle(['tests/only_in_en.ftl', 'tests/main.ftl'], default_locale='en') activate('fr-FR') self.assertEqual(bundle.format('simple'), "Facile")
def test_missing_ftl_file(self): activate('en') bundle = Bundle(['tests/non-existant.ftl'], default_locale='en') self.assertRaises(FileNotFoundError, bundle.format, 'simple')
def test_fallback(self): activate('tr') bundle = Bundle(['tests/main.ftl'], default_locale='en') self.assertEqual(bundle.format('missing-from-others'), "Missing from others")
def setUp(self): activate('en')