def test_rule3(self): """ Basic CharField/TextField test. Could as well call _test_field, just kept for reference. """ title1_de = "title de" title1_en = "title en" n = TestModel.objects.create(title_de=title1_de, title_en=title1_en) self.failUnlessEqual(get_language(), "de") self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.title_de, title1_de) self.failUnlessEqual(n.title_en, title1_en) n.title_de = "Neuer Titel" n.save() self.failUnlessEqual(n.title, n.title_de) # Now switch to "en" trans_real.activate("en") self.failUnlessEqual(get_language(), "en") n.title_en = "New title" # the n.title field is not updated before the instance is saved n.save() self.failUnlessEqual(n.title, n.title_en) trans_real.deactivate()
def update_category_cache(sender, instance, **kwargs): for l in settings.LANGUAGES: l = l[0] trans_real.activate(l) cache.set('%s_%s' % (CATEGORY_LIST_CACHE_KEY, l), Category.objects.filter(is_active=True), CACHE_TIMEOUT) trans_real.deactivate()
def _pre_setup(self): super(TestCase, self)._pre_setup() # XXX See if we can remove this when we switch to Django 1.8. # Some crud from the migration system gets stuck here and we have to # kick it loose manually. from mkt.webapps.models import AddonUser UserProfile.addonuser_set.related.model = AddonUser # Clean the slate. cache.clear() post_request_task._discard_tasks() trans_real.deactivate() trans_real._translations = {} # Django fails to clear this cache. trans_real.activate(settings.LANGUAGE_CODE) self.mock_browser_id() global JINJA_INSTRUMENTED if not JINJA_INSTRUMENTED: import jinja2 old_render = jinja2.Template.render def instrumented_render(self, *args, **kwargs): context = dict(*args, **kwargs) test.signals.template_rendered.send(sender=self, template=self, context=context) return old_render(self, *args, **kwargs) jinja2.Template.render = instrumented_render JINJA_INSTRUMENTED = True
def test_url(self): site_pages = { 'model': models.TestRootPage, 'kwargs': {'title': 'root URL', }, 'children': { 'child1': { 'model': models.TestSlugPage1, 'kwargs': {'title': 'child1 URL', 'slug_de': 'url-de-01', 'slug_en': 'url-en-01'}, }, 'child2': { 'model': models.TestSlugPage2, 'kwargs': {'title': 'child2 URL', 'slug': 'url-de-02'}, }, }, } page_factory.create_page_tree(site_pages) root_page = site_pages['instance'] page_01 = site_pages['children']['child1']['instance'] page_02 = site_pages['children']['child2']['instance'] self.assertEqual(root_page.url, '/de/') self.assertEqual(page_01.url, '/de/url-de-01/') self.assertEqual(page_02.url, '/de/url-de-02/') trans_real.activate('en') self.assertEqual(root_page.url, '/en/') self.assertEqual(page_01.url, '/en/url-en-01/') self.assertEqual(page_02.url, '/en/url-de-02/')
def setUp(self): self._old_language = get_language() trans_real.activate('de') # ensure we have a fresh site cache for language in mt_settings.AVAILABLE_LANGUAGES: cache.delete('wagtail_site_root_paths_{}'.format(language))
def _test_field(self, field_name, value1_de, value1_en, value2, value3, deactivate=True): field_name_de = '%s_de' % field_name field_name_en = '%s_en' % field_name params = {'title_de': 'title de', 'title_en': 'title en'} params[field_name_de] = value1_de params[field_name_en] = value1_en self.failUnlessEqual(get_language(), "de") n = TestModel.objects.create(**params) self.failUnlessEqual(getattr(n, field_name), value1_de) self.failUnlessEqual(getattr(n, field_name_de), value1_de) self.failUnlessEqual(getattr(n, field_name_en), value1_en) setattr(n, field_name, value2) n.save() self.failUnlessEqual(getattr(n, field_name), value2) self.failUnlessEqual(getattr(n, field_name), getattr(n, field_name_de)) trans_real.activate("en") self.failUnlessEqual(get_language(), "en") setattr(n, field_name, value3) setattr(n, field_name_de, value1_de) n.save() self.failUnlessEqual(getattr(n, field_name), value3) self.failUnlessEqual(getattr(n, field_name), getattr(n, field_name_en)) self.failUnlessEqual(value1_de, getattr(n, field_name_de)) if deactivate: trans_real.deactivate()
def _validate_slugs(page): """ Determine whether the given slug is available for use on a child page of parent_page. """ parent_page = page.get_parent() if parent_page is None: # the root page's slug can be whatever it likes... return {} # Save the current active language current_language = get_language() siblings = page.get_siblings(inclusive=False).specific() errors = {} for language in mt_settings.AVAILABLE_LANGUAGES: # Temporarily activate every language because even though there might # be no repeated value for slug_pt the fallback of an empty slug could # already be in use trans_real.activate(language) siblings_slugs = [sibling.slug for sibling in siblings] if page.specific.slug in siblings_slugs: errors[build_localized_fieldname('slug', language)] = _("This slug is already in use") # Re-enable the original language trans_real.activate(current_language) return errors
def test_rule2(self): """ Basic CharField/TextField test. Could as well call _test_field, just kept for reference. """ self.failUnlessEqual(get_language(), "de") title1_de = "title de" title1_en = "title en" n = TestModel.objects.create(title_de=title1_de, title_en=title1_en) self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.title_de, title1_de) self.failUnlessEqual(n.title_en, title1_en) title2 = "Neuer Titel" n.title = title2 n.save() self.failUnlessEqual(n.title, title2) self.failUnlessEqual(n.title, n.title_de) trans_real.activate("en") self.failUnlessEqual(get_language(), "en") title3 = "new title" n.title = title3 n.title_de = title1_de n.save() self.failUnlessEqual(n.title, title3) self.failUnlessEqual(n.title, n.title_en) self.failUnlessEqual(title1_de, n.title_de) trans_real.deactivate()
def _pre_setup(self): super(TestCase, self)._pre_setup() # If we have a settings_test.py let's roll it into our settings. try: import settings_test # Use setattr to update Django's proxies: for k in dir(settings_test): setattr(settings, k, getattr(settings_test, k)) except ImportError: pass # Clean the slate. cache.clear() post_request_task._discard_tasks() trans_real.deactivate() trans_real._translations = {} # Django fails to clear this cache. trans_real.activate(settings.LANGUAGE_CODE) self.mock_browser_id() global JINJA_INSTRUMENTED if not JINJA_INSTRUMENTED: import jinja2 old_render = jinja2.Template.render def instrumented_render(self, *args, **kwargs): context = dict(*args, **kwargs) test.signals.template_rendered.send(sender=self, template=self, context=context) return old_render(self, *args, **kwargs) jinja2.Template.render = instrumented_render JINJA_INSTRUMENTED = True
def _test_field(self, field_name, value1_de, value1_en, value2, value3, deactivate=True): field_name_de = '%s_de' % field_name field_name_en = '%s_en' % field_name params = {'title_de': 'title de', 'title_en': 'title en'} params[field_name_de] = value1_de params[field_name_en] = value1_en n = TestModel.objects.create(**params) self.failUnlessEqual(get_language(), "de") self.failUnlessEqual(getattr(n, field_name), value1_de) self.failUnlessEqual(getattr(n, field_name_de), value1_de) self.failUnlessEqual(getattr(n, field_name_en), value1_en) setattr(n, field_name, value2) n.save() self.failUnlessEqual(getattr(n, field_name), getattr(n, field_name_de)) # Now switch to "en" trans_real.activate("en") self.failUnlessEqual(get_language(), "en") setattr(n, field_name_en, value3) # the n.title field is not updated before the instance is saved n.save() self.failUnlessEqual(getattr(n, field_name), getattr(n, field_name_en)) if deactivate: trans_real.deactivate()
def _test_field(self, field_name, value_de, value_en, deactivate=True): field_name_de = '%s_de' % field_name field_name_en = '%s_en' % field_name params = {'title_de': 'title de', 'title_en': 'title en'} params[field_name_de] = value_de params[field_name_en] = value_en n = TestModel.objects.create(**params) # Language is set to "de" at this point self.failUnlessEqual(get_language(), "de") self.failUnlessEqual(getattr(n, field_name), value_de) self.failUnlessEqual(getattr(n, field_name_de), value_de) self.failUnlessEqual(getattr(n, field_name_en), value_en) # Now switch to "en" trans_real.activate("en") self.failUnlessEqual(get_language(), "en") # Should now be return the english one (just by switching the language) self.failUnlessEqual(getattr(n, field_name), value_en) n = TestModel.objects.create(**params) n.save() # Language is set to "en" at this point self.failUnlessEqual(getattr(n, field_name), value_en) self.failUnlessEqual(getattr(n, field_name_de), value_de) self.failUnlessEqual(getattr(n, field_name_en), value_en) trans_real.activate("de") self.failUnlessEqual(get_language(), "de") self.failUnlessEqual(getattr(n, field_name), value_de) if deactivate: trans_real.deactivate()
def test_slugurl_trans(self): """ Assert tag slugurl_trans is immune to user's current language """ from wagtail_modeltranslation.templatetags.wagtail_modeltranslation import slugurl_trans site_pages = { 'model': models.TestRootPage, 'kwargs': {'title': 'root slugurl', }, 'children': { 'child': { 'model': models.TestSlugPage1, 'kwargs': {'title': 'child slugurl', 'slug': 'child-slugurl', 'slug_en': 'child-slugurl-en'}, 'children': {}, }, }, } site = page_factory.create_page_tree(site_pages) request_mock = request_factory.get('/') setattr(request_mock, 'site', site) context = {'request': request_mock} self.assertEqual(slugurl_trans(context, 'root-slugurl'), '/de/') self.assertEqual(slugurl_trans(context, 'child-slugurl'), '/de/child-slugurl/') self.assertEqual(slugurl_trans(context, 'child-slugurl-en', 'en'), '/de/child-slugurl/') trans_real.activate('en') self.assertEqual(slugurl_trans(context, 'root-slugurl'), '/en/') self.assertEqual(slugurl_trans(context, 'child-slugurl'), '/en/child-slugurl-en/') self.assertEqual(slugurl_trans(context, 'child-slugurl-en', 'en'), '/en/child-slugurl-en/')
def _pre_setup(self): super(TestCase, self)._pre_setup() # Clean the slate. cache.clear() post_request_task._discard_tasks() trans_real.deactivate() trans_real._translations = {} # Django fails to clear this cache. trans_real.activate(settings.LANGUAGE_CODE) self.mock_browser_id() global JINJA_INSTRUMENTED if not JINJA_INSTRUMENTED: import jinja2 old_render = jinja2.Template.render def instrumented_render(self, *args, **kwargs): context = dict(*args, **kwargs) test.signals.template_rendered.send(sender=self, template=self, context=context) return old_render(self, *args, **kwargs) jinja2.Template.render = instrumented_render JINJA_INSTRUMENTED = True
def test_rule2(self): """ Rule 2: Assigning a value to the original field also updates the value in the associated translation field of the default language """ self.failUnlessEqual(get_language(), "de") title1_de = "title de" title1_en = "title en" n = TestModel.objects.create(title_de=title1_de, title_en=title1_en) self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.title_de, title1_de) self.failUnlessEqual(n.title_en, title1_en) title2 = "Neuer Titel" n.title = title2 n.save() self.failUnlessEqual(n.title, title2) self.failUnlessEqual(n.title, n.title_de) trans_real.activate("en") self.failUnlessEqual(get_language(), "en") title3 = "new title" n.title = title3 n.title_de = title1_de n.save() self.failUnlessEqual(n.title, title3) self.failUnlessEqual(n.title, n.title_en) self.failUnlessEqual(title1_de, n.title_de) trans_real.deactivate()
def test_rule3(self): """ Rule 3: Assigning a value to a translation field of the default language also updates the original field - note that the value of the original field will not be updated until the model instance is saved. """ title1_de = "title de" title1_en = "title en" n = TestModel.objects.create(title_de=title1_de, title_en=title1_en) self.failUnlessEqual(get_language(), "de") self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.title_de, title1_de) self.failUnlessEqual(n.title_en, title1_en) n.title_de = "Neuer Titel" n.save() self.failUnlessEqual(n.title, n.title_de) # Now switch to "en" trans_real.activate("en") self.failUnlessEqual(get_language(), "en") n.title_en = "New title" # the n.title field is not updated before the instance is saved n.save() self.failUnlessEqual(n.title, n.title_en) trans_real.deactivate()
def _validate_slugs(page): """ Determine whether the given slug is available for use on a child page of parent_page. """ parent_page = page.get_parent() if parent_page is None: # the root page's slug can be whatever it likes... return {} # Save the current active language current_language = get_language() siblings = page.get_siblings(inclusive=False) errors = {} for language in mt_settings.AVAILABLE_LANGUAGES: # Temporarily activate every language because even though there might # be no repeated value for slug_pt the fallback of an empty slug could # already be in use trans_real.activate(language) siblings_slugs = [sibling.slug for sibling in siblings] if page.slug in siblings_slugs: errors[build_localized_fieldname( 'slug', language)] = _("This slug is already in use") # Re-enable the original language trans_real.activate(current_language) return errors
def send_reminders_for_wetmills(xform, wetmills, context, default_text): router = get_router() # for each wetmill for wetmill in wetmills: context['wetmill'] = wetmill # send to all accountants for accountant in Accountant.objects.filter(wetmill=wetmill): # activate our language if there is one trans_real.activate(accountant.language) text = Blurb.get(xform, 'accountant-reminder', context, default_text) # and send it off router.add_outgoing(accountant.connection, text) # and all observers for observer in WetmillObserver.objects.filter(wetmill=wetmill): # activate our language if there is one trans_real.activate(observer.language) text = Blurb.get(xform, 'observer-reminder', context, default_text) # and send it off router.add_outgoing(observer.connection, text) activate('en-us')
def import_classification_of_icd_cid(file_name): filename = os.path.join(settings.BASE_DIR, os.path.join("..", "..", os.path.join("resources", "load-idc-table", file_name))) increment = 2 #start of second line, without header with open(filename) as csvfile: total_lines = sum(1 for row in csvfile) if total_lines: pass else: return print('File Empty') with open(filename, 'r') as csvFile: reader = csv.reader(csvFile) next(reader, None) for row in reader: classifications_of_diseases = ClassificationOfDiseases.objects.create( code=row[0], description=row[2], abbreviated_description=row[3] ) # colunas _en activate("en") classifications_of_diseases.description = row[1] classifications_of_diseases.abbreviated_description = row[1] classifications_of_diseases.save() deactivate() progress_bar(increment, total_lines, status='Updating database') increment += 1 print('\n')
def import_classification_of_icd_cid(file_name): filename = os.path.join( settings.BASE_DIR, os.path.join("..", "..", os.path.join("resources", "load-idc-table", file_name))) increment = 2 # start of second line, without header with open(filename) as csvfile: total_lines = sum(1 for row in csvfile) if total_lines: pass else: return print('File Empty') with open(filename, 'r') as csvFile: reader = csv.reader(csvFile) next(reader, None) for row in reader: classifications_of_diseases = ClassificationOfDiseases.objects.create( code=row[0], description=row[2], abbreviated_description=row[3]) # colunas _en activate("en") classifications_of_diseases.description = row[1] classifications_of_diseases.abbreviated_description = row[1] classifications_of_diseases.save() deactivate() progress_bar(increment, total_lines, status='Updating database') increment += 1 print('\n')
def _pre_setup(self): super(KumaTestCase, self)._pre_setup() # Clean the slate. cache.clear() memcache.clear() trans_real.deactivate() trans_real._translations = {} # Django fails to clear this cache. trans_real.activate(settings.LANGUAGE_CODE) global JINJA_INSTRUMENTED if not JINJA_INSTRUMENTED: import jinja2 old_render = jinja2.Template.render def instrumented_render(self, *args, **kwargs): context = dict(*args, **kwargs) test.signals.template_rendered.send(sender=self, template=self, context=context) return old_render(self, *args, **kwargs) jinja2.Template.render = instrumented_render JINJA_INSTRUMENTED = True
def set_language(self, request): set_language = request.data.get('language', False) back_language = dict(settings.LANGUAGES) if set_language in back_language: language = set_language else: language = settings.LANGUAGE_CODE trans_real.activate(language)
def _pre_setup(self): """Adjust cache-machine settings, and send custom pre-setup signal.""" signals.pre_setup.send(sender=self.__class__) settings.CACHE_COUNT_TIMEOUT = None trans_real.deactivate() trans_real._translations = {} # Django fails to clear this cache. trans_real.activate(settings.LANGUAGE_CODE) super(TestCase, self)._pre_setup()
def _pre_setup(self): super(KumaTestMixin, self)._pre_setup() # Clean the slate. cache.clear() trans_real.deactivate() trans_real._translations = {} # Django fails to clear this cache. trans_real.activate(settings.LANGUAGE_CODE)
def test_relative_url(self): try: from wagtail.core.models import Site except ImportError: from wagtail.wagtailcore.models import Site # Create a test Site with a root page root = models.TestRootPage(title='title slugurl', depth=1, path='0004', slug_en='title_slugurl_en', slug_de='title_slugurl_de') root.save() site = Site(root_page=root) site.save() # Add children to the root child = root.add_child( instance=models.TestSlugPage1(title='child1 slugurl', slug_en='child-slugurl-en', slug_de='child-slugurl-de', depth=2, path='00040001')) child.save_revision().publish() url_1_de = child.relative_url(site) self.assertEqual( url_1_de, '/de/child-slugurl-de/', 'When using the default language, slugurl produces the wrong url.') trans_real.activate('en') url_1_en = child.relative_url(site) self.assertEqual( url_1_en, '/en/child-slugurl-en/', 'When using non-default language, slugurl produces the wrong url.') # Add children using non-default language child2 = root.add_child( instance=models.TestSlugPage2(title='child2 slugurl', title_de='child2 slugurl DE', slug_de='child2-slugurl-de', slug_en='child2-slugurl-en', depth=2, path='00040002')) child2.save_revision().publish() url_2_en = child2.relative_url(site) self.assertEqual( url_2_en, '/en/child2-slugurl-en/', 'When using non-default language, slugurl produces the wrong url.') trans_real.activate('de') url_2_de = child2.relative_url(site) self.assertEqual( url_2_de, '/de/child2-slugurl-de/', 'When using non-default language, slugurl produces the wrong url.')
def test_setting_translation(self): s = Setting.objects.create() s.site_name = "Example name" s.save() trans_real.activate('es') s.site_name = "Example in Spanish" s.save() self.assertEquals(get_setting('site_name', ''), s.site_name)
def handle(self, *args, **kwargs): activate('sl') for a in kategorije: thumbnails(a) #thumbnails('grafike') addDefaultTypes() addImagesFromDirectory() addDefaultTechniques() addDefaultTags() addTextsFromDirectory()
def setUp(self): trans_real.activate('en') # adding these pages in setup, as re-creating the fixture test.json is too much hassle # self.add_page('home') # self.add_page('web') # self.add_page('data') # self.add_page('digitisation') self.superuser = self.create_superuser()
def pre_save(self, model_instance, add): """ Returns slug field's value using the language set by `WAGTAILMODELTRANSLATION_ORIGINAL_SLUG_LANGUAGE` just before saving. """ current_language = get_language() # using ORIGINAL_SLUG_LANGUAGE makes Page's slug value consistent trans_real.activate(reference_slug_language) value = getattr(model_instance, self.attname) trans_real.activate(current_language) return value
def test_streamfield_fallback(self): body_text = '[{"value": "Some text", "type": "text"}]' page = models.StreamFieldPanelPage(title='Streamfield Fallback', slug='streamfield_fallback', depth=1, path='0005', body=body_text) page.save() self.assertEqual(str(page.body), '<div class="block-text">Some text</div>') trans_real.activate('en') self.assertEqual(str(page.body), '<div class="block-text">Some text</div>', 'page.body did not fallback to original language.')
def test_root_page_slug(self): site_pages = { 'model': models.TestRootPage, 'kwargs': {'title': 'root URL', 'slug_de': 'root-de', 'slug_en': 'root-en'}, 'children': { 'child1': { 'model': models.TestSlugPage1, 'kwargs': {'title': 'child1 URL', 'slug_de': 'url-de-01', 'slug_en': 'url-en-01'}, }, 'child2': { 'model': models.TestSlugPage2, 'kwargs': {'title': 'child2 URL', 'slug': 'url-de-02'}, }, 'child3': { 'model': models.TestSlugPage2, 'kwargs': {'title': 'child3 URL', 'slug': 'url-de-03'}, }, }, } page_factory.create_page_tree(site_pages) request = HttpRequest() site_root_page = site_pages['instance'] wagtail_page_01 = site_pages['children']['child1']['instance'] wagtail_page_02 = site_pages['children']['child2']['instance'] wagtail_page_03 = site_pages['children']['child3']['instance'] self.assertEqual(wagtail_page_01.url, '/de/url-de-01/') self.assertEqual(wagtail_page_01.url_path, '/root-de/url-de-01/') if VERSION >= (1, 11): self.assertEqual(wagtail_page_02.get_url(request=request), '/de/url-de-02/') # with request trans_real.activate('en') self.assertEqual(wagtail_page_01.url, '/en/url-en-01/') self.assertEqual(wagtail_page_01.url_path, '/root-en/url-en-01/') if VERSION >= (1, 11): self.assertEqual(wagtail_page_02.get_url(request=request), '/en/url-de-02/') trans_real.activate('de') # new request after changing language self.assertEqual(wagtail_page_03.url, '/de/url-de-03/') if VERSION >= (1, 11): self.assertEqual(wagtail_page_01.get_url(request=HttpRequest()), '/de/url-de-01/') # URL should not be broken after updating the root_page (ensure the cache is evicted) self.assertEqual(wagtail_page_01.url, '/de/url-de-01/') site_root_page.slug = 'new-root-de' site_root_page.save() wagtail_page_01_new = site_root_page.get_children().get(id=wagtail_page_01.id) self.assertEqual(wagtail_page_01_new.url, '/de/url-de-01/')
def test_rule3(self): """ Basic CharField/TextField test. Could as well call _test_field, just kept for reference. """ title1_de = "title de" title1_en = "title en" n = TestModel.objects.create(title_de=title1_de, title_en=title1_en) self.failUnlessEqual(get_language(), 'de') self.failUnlessEqual(mt_settings.DEFAULT_LANGUAGE, 'de') self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.title_de, title1_de) self.failUnlessEqual(n.title_en, title1_en) n.title_de = "Neuer Titel" n.save() # We expect that the original field holds the same value as the german # one (german is the default language). self.failUnlessEqual(n.title, n.title_de) # Fetch the updated object and verify all fields updated_obj = TestModel.objects.get(id=n.id) self.failUnlessEqual(updated_obj.title, 'Neuer Titel') self.failUnlessEqual(updated_obj.title_de, 'Neuer Titel') self.failUnlessEqual(updated_obj.title_en, 'title en') # Now switch to "en" trans_real.activate("en") self.failUnlessEqual(get_language(), "en") n.title_en = "New title" # the n.title field is not updated before the instance is saved n.save() # We expect that the original field has *not* been changed as german # is the default language and we only changed the value of the english # field. # FIXME: Demonstrates a wrong behaviour of save when the current # language is different than the default language. In this case the # original field is set to value of the current language's field. # See issue 33 for details. # TODO: Reactivate, temporarily deactivated for a full run of travis ci #self.failUnlessEqual(n.title, n.title_de) # Fetch the updated object and verify all fields #updated_obj = TestModel.objects.get(id=n.id) #self.failUnlessEqual(updated_obj.title, 'Neuer Titel') #self.failUnlessEqual(updated_obj.title_de, 'Neuer Titel') #self.failUnlessEqual(updated_obj.title_en, 'New title') trans_real.deactivate()
def test_rule1(self): """ Basic CharField/TextField test. Could as well call _test_field, just kept for reference. """ title1_de = "title de" title1_en = "title en" text_de = "Dies ist ein deutscher Satz" text_en = "This is an english sentence" n = TestModel.objects.create(title_de=title1_de, title_en=title1_en, text_de=text_de, text_en=text_en) n.save() # Language is set to 'de' at this point self.failUnlessEqual(get_language(), 'de') self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.title_de, title1_de) self.failUnlessEqual(n.title_en, title1_en) self.failUnlessEqual(n.text, text_de) self.failUnlessEqual(n.text_de, text_de) self.failUnlessEqual(n.text_en, text_en) # Now switch to "en" trans_real.activate("en") self.failUnlessEqual(get_language(), "en") # Title should now be return the english one (just by switching the # language) self.failUnlessEqual(n.title, title1_en) self.failUnlessEqual(n.text, text_en) n = TestModel.objects.create(title_de=title1_de, title_en=title1_en, text_de=text_de, text_en=text_en) n.save() # Language is set to "en" at this point self.failUnlessEqual(n.title, title1_en) self.failUnlessEqual(n.title_de, title1_de) self.failUnlessEqual(n.title_en, title1_en) self.failUnlessEqual(n.text, text_en) self.failUnlessEqual(n.text_de, text_de) self.failUnlessEqual(n.text_en, text_en) trans_real.activate('de') self.failUnlessEqual(get_language(), 'de') self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.text, text_de) trans_real.deactivate()
def test_fallback_values_1(self): """ If ``fallback_values`` is set to string, all untranslated fields would return this string. """ title1_de = "title de" n = TestModelWithFallback() n.title = title1_de n.save() del n n = TestModelWithFallback.objects.get(title=title1_de) self.failUnlessEqual(n.title, title1_de) trans_real.activate("en") self.failUnlessEqual(n.title, "")
def test_rule1(self): """ Rule 1: Reading the value from the original field returns the value in translated to the current language. """ title1_de = "title de" title1_en = "title en" text_de = "Dies ist ein deutscher Satz" text_en = "This is an english sentence" # Test 1. n = TestModel.objects.create(title_de=title1_de, title_en=title1_en, text_de=text_de, text_en=text_en) n.save() # language is set to "de" at this point self.failUnlessEqual(get_language(), "de") self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.title_de, title1_de) self.failUnlessEqual(n.title_en, title1_en) self.failUnlessEqual(n.text, text_de) self.failUnlessEqual(n.text_de, text_de) self.failUnlessEqual(n.text_en, text_en) # Now switch to "en" trans_real.activate("en") self.failUnlessEqual(get_language(), "en") # Title should now be return the english one (just by switching the # language) self.failUnlessEqual(n.title, title1_en) self.failUnlessEqual(n.text, text_en) n = TestModel.objects.create(title_de=title1_de, title_en=title1_en, text_de=text_de, text_en=text_en) n.save() # language is set to "en" at this point self.failUnlessEqual(n.title, title1_en) self.failUnlessEqual(n.title_de, title1_de) self.failUnlessEqual(n.title_en, title1_en) self.failUnlessEqual(n.text, text_en) self.failUnlessEqual(n.text_de, text_de) self.failUnlessEqual(n.text_en, text_en) trans_real.activate("de") self.failUnlessEqual(get_language(), "de") self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.text, text_de) trans_real.deactivate()
def test_rule3(self): """ Basic CharField/TextField test. Could as well call _test_field, just kept for reference. """ title1_de = "title de" title1_en = "title en" n = TestModel.objects.create(title_de=title1_de, title_en=title1_en) self.failUnlessEqual(get_language(), 'de') self.failUnlessEqual(DEFAULT_LANGUAGE, 'de') self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.title_de, title1_de) self.failUnlessEqual(n.title_en, title1_en) n.title_de = "Neuer Titel" n.save() # We expect that the original field holds the same value as the german # one (german is the default language). self.failUnlessEqual(n.title, n.title_de) # Fetch the updated object and verify all fields updated_obj = TestModel.objects.get(id=n.id) self.failUnlessEqual(updated_obj.title, 'Neuer Titel') self.failUnlessEqual(updated_obj.title_de, 'Neuer Titel') self.failUnlessEqual(updated_obj.title_en, 'title en') # Now switch to "en" trans_real.activate("en") self.failUnlessEqual(get_language(), "en") n.title_en = "New title" # the n.title field is not updated before the instance is saved n.save() # We expect that the original field has *not* been changed as german # is the default language and we only changed the value of the english # field. # FIXME: Demonstrates a wrong behaviour of save when the current # language is different than the default language. In this case the # original field is set to value of the current language's field. # See issue 33 for details. self.failUnlessEqual(n.title, n.title_de) # Fetch the updated object and verify all fields updated_obj = TestModel.objects.get(id=n.id) self.failUnlessEqual(updated_obj.title, 'Neuer Titel') self.failUnlessEqual(updated_obj.title_de, 'Neuer Titel') self.failUnlessEqual(updated_obj.title_en, 'New title') trans_real.deactivate()
def test_relative_url(self): try: from wagtail.core.models import Site except ImportError: from wagtail.wagtailcore.models import Site # Create a test Site with a root page root = models.TestRootPage(title='title slugurl', depth=1, path='0004', slug_en='title_slugurl_en', slug_de='title_slugurl_de') root.save() site = Site(root_page=root) site.save() # Add children to the root child = root.add_child( instance=models.TestSlugPage1(title='child1 slugurl', slug_en='child-slugurl-en', slug_de='child-slugurl-de', depth=2, path='00040001') ) child.save_revision().publish() url_1_de = child.relative_url(site) self.assertEqual(url_1_de, '/de/child-slugurl-de/', 'When using the default language, slugurl produces the wrong url.') trans_real.activate('en') url_1_en = child.relative_url(site) self.assertEqual(url_1_en, '/en/child-slugurl-en/', 'When using non-default language, slugurl produces the wrong url.') # Add children using non-default language child2 = root.add_child( instance=models.TestSlugPage2(title='child2 slugurl', title_de='child2 slugurl DE', slug_de='child2-slugurl-de', slug_en='child2-slugurl-en', depth=2, path='00040002') ) child2.save_revision().publish() url_2_en = child2.relative_url(site) self.assertEqual(url_2_en, '/en/child2-slugurl-en/', 'When using non-default language, slugurl produces the wrong url.') trans_real.activate('de') url_2_de = child2.relative_url(site) self.assertEqual(url_2_de, '/de/child2-slugurl-de/', 'When using non-default language, slugurl produces the wrong url.')
def test_fallback_values_2(self): """ If ``fallback_values`` is set to ``dict``, all untranslated fields in ``dict`` would return this mapped value. Fields not in ``dict`` would return default translation. """ title1_de = "title de" text1_de = "text in german" n = TestModelWithFallback2() n.title = title1_de n.text = text1_de n.save() del n n = TestModelWithFallback2.objects.get(title=title1_de) trans_real.activate("en") self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.text, TestTranslationOptionsWithFallback2.fallback_values["text"])
def test_rule1(self): """ Basic CharField/TextField test. Could as well call _test_field, just kept for reference. """ title1_de = "title de" title1_en = "title en" text_de = "Dies ist ein deutscher Satz" text_en = "This is an english sentence" n = TestModel.objects.create(title_de=title1_de, title_en=title1_en, text_de=text_de, text_en=text_en) n.save() # Language is set to "de" at this point self.failUnlessEqual(get_language(), "de") self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.title_de, title1_de) self.failUnlessEqual(n.title_en, title1_en) self.failUnlessEqual(n.text, text_de) self.failUnlessEqual(n.text_de, text_de) self.failUnlessEqual(n.text_en, text_en) # Now switch to "en" trans_real.activate("en") self.failUnlessEqual(get_language(), "en") # Title should now be return the english one (just by switching the # language) self.failUnlessEqual(n.title, title1_en) self.failUnlessEqual(n.text, text_en) n = TestModel.objects.create(title_de=title1_de, title_en=title1_en, text_de=text_de, text_en=text_en) n.save() # Language is set to "en" at this point self.failUnlessEqual(n.title, title1_en) self.failUnlessEqual(n.title_de, title1_de) self.failUnlessEqual(n.title_en, title1_en) self.failUnlessEqual(n.text, text_en) self.failUnlessEqual(n.text_de, text_de) self.failUnlessEqual(n.text_en, text_en) trans_real.activate("de") self.failUnlessEqual(get_language(), "de") self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.text, text_de) trans_real.deactivate()
def test_fallback_values_2(self): """ If ``fallback_values`` is set to ``dict``, all untranslated fields in ``dict`` would return this mapped value. Fields not in ``dict`` would return default translation. """ title1_de = "title de" text1_de = "text in german" n = TestModelWithFallback2() n.title = title1_de n.text = text1_de n.save() del n n = TestModelWithFallback2.objects.get(title=title1_de) trans_real.activate("en") self.failUnlessEqual(n.title, title1_de) self.failUnlessEqual(n.text,\ TestTranslationOptionsWithFallback2.fallback_values['text'])
def icd_english_translation(tree): activate("en") codes = [] icd_list = {} categories = tree.findall(".//Class/[@kind='category']") for category in categories: code = category.attrib['code'] code = code.replace(".", "") label_element = category.find("Rubric[@kind='preferred']").find( "Label") abbreviated_description = format_text_element(label_element) long_description = category.find("Rubric[@kind='preferredLong']") if long_description: description = format_text_element(long_description.find("Label")) else: description = abbreviated_description icd_list[code] = [abbreviated_description, description] codes.append(code) classifications_of_diseases = ClassificationOfDiseases.objects.filter( code__in=codes) records_updated = 0 for classification_of_disease in classifications_of_diseases: classification_of_disease.abbreviated_description = icd_list[ classification_of_disease.code][0] classification_of_disease.description = icd_list[ classification_of_disease.code][1] classification_of_disease.save() records_updated += 1 deactivate() return records_updated
def merge_translation(self, context): """ Context wrapper which modifies the given language's translation catalog using the i18n service, if found. """ language = get_language() i18n_service = context.get('_i18n_service', None) if i18n_service: # Cache the original translation object to reduce overhead if language not in self._translations: self._translations[language] = trans_real.DjangoTranslation(language) translation = trans_real.translation(language) translation.merge(i18n_service) yield # Revert to original translation object if language in self._translations: trans_real._translations[language] = self._translations[language] # Re-activate the current language to reset translation caches trans_real.activate(language)
def icd_english_translation(tree): activate("en") codes = [] icd_list = {} categories = tree.findall(".//Class/[@kind='category']") for category in categories: code = category.attrib['code'] code = code.replace(".", "") label_element = category.find("Rubric[@kind='preferred']").find("Label") abbreviated_description = format_text_element(label_element) long_description = category.find("Rubric[@kind='preferredLong']") if long_description: description = format_text_element(long_description.find("Label")) else: description = abbreviated_description icd_list[code] = [abbreviated_description, description] codes.append(code) classifications_of_diseases = ClassificationOfDiseases.objects.filter(code__in=codes) records_updated = 0 for classification_of_disease in classifications_of_diseases: classification_of_disease.abbreviated_description = icd_list[classification_of_disease.code][0] classification_of_disease.description = icd_list[classification_of_disease.code][1] classification_of_disease.save() records_updated += 1 deactivate() return records_updated