def test_hook_i18n(self): actions.update_course_config( COURSE_NAME, { 'html_hooks': {'base': {'after_body_tag_begins': 'foozle'}}, 'extra_locales': [ {'locale': 'de', 'availability': 'available'}, ] }) hook_bundle = { 'content': { 'type': 'html', 'source_value': '', 'data': [{ 'source_value': 'foozle', 'target_value': 'FUZEL', }], } } hook_key = i18n_dashboard.ResourceBundleKey( utils.ResourceHtmlHook.TYPE, 'base.after_body_tag_begins', 'de') with common_utils.Namespace(NAMESPACE): i18n_dashboard.ResourceBundleDAO.save( i18n_dashboard.ResourceBundleDTO(str(hook_key), hook_bundle)) # Verify non-translated version. response = self.get(BASE_URL) dom = self.parse_html_string(response.body) html_hook = dom.find('.//div[@id="base-after-body-tag-begins"]') self.assertEquals('foozle', html_hook.text) # Set preference to translated language, and check that that's there. with common_utils.Namespace(NAMESPACE): prefs = models.StudentPreferencesDAO.load_or_default() prefs.locale = 'de' models.StudentPreferencesDAO.save(prefs) response = self.get(BASE_URL) dom = self.parse_html_string(response.body) html_hook = dom.find('.//div[@id="base-after-body-tag-begins"]') self.assertEquals('FUZEL', html_hook.text) # With no translation present, but preference set to foreign language, # verify that we fall back to the original language. # Remove translation bundle, and clear cache. with common_utils.Namespace(NAMESPACE): i18n_dashboard.ResourceBundleDAO.delete( i18n_dashboard.ResourceBundleDTO(str(hook_key), hook_bundle)) model_caching.CacheFactory.get_cache_instance( i18n_dashboard.RESOURCE_BUNDLE_CACHE_NAME).clear() response = self.get(BASE_URL) dom = self.parse_html_string(response.body) html_hook = dom.find('.//div[@id="base-after-body-tag-begins"]') self.assertEquals('foozle', html_hook.text)
def _add_announcement_and_translation(self, locale, is_draft=False): announcement = announcements.AnnouncementEntity() announcement.title = 'Test Announcement' announcement.html = 'Announcement Content' announcement.is_draft = is_draft announcement.put() key = i18n_dashboard.ResourceBundleKey( announcements.ResourceHandlerAnnouncement.TYPE, announcement.key().id(), locale) dto = i18n_dashboard.ResourceBundleDTO( str(key), { 'title': { 'type': 'string', 'source_value': '', 'data': [{ 'source_value': 'Test Announcement', 'target_value': 'TEST ANNOUNCEMENT' }] }, 'html': { 'type': 'string', 'source_value': '', 'data': [{ 'source_value': 'Announcement Content', 'target_value': 'ANNOUNCEMENT CONTENT' }] }, }) i18n_dashboard.ResourceBundleDAO.save(dto) return announcement
def test_localized_search(self): def _text(elt): return ''.join(elt.itertext()) dogs_page = """ <html> <body> A page about dogs. </body> </html>""" dogs_link = 'http://dogs.null/' self.pages[dogs_link + '$'] = (dogs_page, 'text/html') dogs_page_fr = """ <html> <body> A page about French dogs. </body> </html>""" dogs_link_fr = 'http://dogs_fr.null/' self.pages[dogs_link_fr + '$'] = (dogs_page_fr, 'text/html') self.base = '/test' context = actions.simple_add_course('test', '*****@*****.**', 'Test Course') course = courses.Course(None, context) actions.login('*****@*****.**') actions.register(self, 'Some Admin') unit = course.add_unit() unit.availability = courses.AVAILABILITY_AVAILABLE lesson = course.add_lesson(unit) lesson.objectives = 'A lesson about <a href="%s">dogs</a>' % dogs_link lesson.availability = courses.AVAILABILITY_AVAILABLE course.save() lesson_bundle = { 'objectives': { 'type': 'html', 'source_value': ('A lesson about <a href="%s">dogs</a>' % dogs_link), 'data': [{ 'source_value': ('A lesson about <a#1 href="%s">dogs</a#1>' % dogs_link), 'target_value': ('A lesson about French <a#1 href="%s">' 'dogs</a#1>' % dogs_link_fr) }] } } lesson_key_fr = i18n_dashboard.ResourceBundleKey( resources_display.ResourceLesson.TYPE, lesson.lesson_id, 'fr') with common_utils.Namespace('ns_test'): i18n_dashboard.ResourceBundleDAO.save( i18n_dashboard.ResourceBundleDTO(str(lesson_key_fr), lesson_bundle)) extra_locales = [{'locale': 'fr', 'availability': 'available'}] with actions.OverriddenEnvironment({'extra_locales': extra_locales}): self.index_test_course() dom = self.parse_html_string(self.get('search?query=dogs').body) snippets = dom.findall( './/div[@class="gcb-search-result-snippet"]') self.assertEquals(2, len(snippets)) # Expect no French hits self.assertIn('page about dogs', _text(snippets[0])) self.assertIn('lesson about dogs', _text(snippets[1])) # Switch locale to 'fr' with common_utils.Namespace('ns_test'): prefs = models.StudentPreferencesDAO.load_or_default() prefs.locale = 'fr' models.StudentPreferencesDAO.save(prefs) dom = self.parse_html_string(self.get('search?query=dogs').body) snippets = dom.findall( './/div[@class="gcb-search-result-snippet"]') self.assertEquals(2, len(snippets)) # Expect no Engish hits self.assertIn('page about French dogs', _text(snippets[0])) self.assertIn('lesson about French dogs', _text(snippets[1]))