def test_translation_priority(self): """Verify that the priority option in GPClient.translation works""" acc = common.get_gpserviceaccount() client = GPClient(acc) languages=['fr'] # prioritize local t = client.translation(bundleId=common.bundleId1, languages=languages, priority='local', domain='messages', localedir='test/data/translations', class_=None, codeset=None) _ = t.gettext value = _('greet') common.my_assert_equal(self, 'greet in French (local)', value, 'incorrect value; should have returned local translation') # prioritize gp t = client.translation(bundleId=common.bundleId1, languages=languages, priority='gp', domain='messages', localedir='test/data/translations', class_=None, codeset=None) _ = t.gettext value = _('greet') common.my_assert_equal(self, 'Bonjour', value, 'incorrect value; should have returned gp translation')
def demo(): ## 1 - create GPServiceAccount acc = None try: acc = GPServiceAccount() except AssertionError: logging.error('Unable to create GPServiceAccount') return ## 2 - create Globalization Pipeline client client = GPClient(acc) # bundle containing the source strings bundleId='demo' languages = client.get_avaliable_languages(bundleId=bundleId) messages = [] for language in languages: ## 3 - create translation instance # Note: because we are displaying the translations for all avalible # languages, a new translation instance is create each time; # normally, only one instance for the language of choice is required t = client.translation(bundleId=bundleId, languages=[language]) _ = t.gettext # get the translated value from Globalization Pipeline service welcomeValue = _('welcome') messages.append({'language': language, 'welcome': welcomeValue}) return render_template('index.html', messages=messages)
def test_translation_priority(self): """Verify that the priority option in GPClient.translation works""" acc = common.get_gpserviceaccount() client = GPClient(acc) languages = ['fr'] # prioritize local t = client.translation(bundleId=common.bundleId1, languages=languages, priority='local', domain='messages', localedir='test/data/translations', class_=None, codeset=None) _ = t.gettext value = _('greet') common.my_assert_equal( self, 'greet in French (local)', value, 'incorrect value; should have returned local translation') # prioritize gp t = client.translation(bundleId=common.bundleId1, languages=languages, priority='gp', domain='messages', localedir='test/data/translations', class_=None, codeset=None) _ = t.gettext value = _('greet') common.my_assert_equal( self, 'Salut', value, 'incorrect value; should have returned gp translation')
def test_local_fallback(self): """Verify local translations are used with expected""" acc = common.get_gpserviceaccount() client = GPClient(acc) languages=['fo', 'fr'] t = client.translation(bundleId=common.bundleId1, languages=languages, priority='local', domain='messages', localedir='test/data/translations', class_=None, codeset=None) _ = t.gettext value = _('greet') common.my_assert_equal(self, 'greet in French (local)', value, 'incorrect value; should have returned local translation')
def root(): logging.info('Processing request') # 1 - create GPServiceAccount # if the app is running on Bluemix, the credentials will be obtained # from the VCAP environment variable acc = None try: acc = GPServiceAccount() except AssertionError: logging.error('Unable to create GPServiceAccount', exc_info=True) return # 2 - create Globalization Pipeline client # the client is responsible for communication with the service client = GPClient(acc) logging.info('GP Client setup complete') bundleId = 'demo' # for the demo, get all avalible languages in the bundle # normally, this should equal to the language of the locale languages = client.get_avaliable_languages(bundleId=bundleId) messages = [] for language in languages: # 3 - create translation instance # Note: because we are displaying the translations for all avalible # languages, a new translation instance is create each time; # normally, only one instance for the language of choice is required t = client.translation(bundleId=bundleId, languages=[language]) # 4 - create a shortcut for the function _ = t.gettext # 5 - get the translated value from Globalization Pipeline service # by calling the shortcut translatedValue = _('welcome') messages.append({'language': language, 'welcome': translatedValue}) return render_template('index.html', messages=messages)
def test_local_translations(self): """Verify local translations are used with expected""" acc = common.get_gpserviceaccount() client = GPClient(acc) languages = ['fr'] t = client.translation(bundleId=common.bundleId1, languages=languages, priority='local', domain='messages', localedir='test/data/translations', class_=None, codeset=None) _ = t.gettext value = _('greet') common.my_assert_equal( self, 'greet in French (local)', value, 'incorrect value; should have returned local translation')
def root(): # 1 - create GPServiceAccount # if the app is running on Bluemix, the credentials will be obtained # from the VCAP environment variable acc = None try: acc = GPServiceAccount() except AssertionError: logging.error('Unable to create GPServiceAccount') return # 2 - create Globalization Pipeline client # the client is responsible for communication with the service client = GPClient(acc) bundleId='demo' # for the demo, get all avalible languages in the bundle # normally, this should equal to the language of the locale languages = client.get_avaliable_languages(bundleId=bundleId) messages = [] for language in languages: # 3 - create translation instance # Note: because we are displaying the translations for all avalible # languages, a new translation instance is create each time; # normally, only one instance for the language of choice is required t = client.translation(bundleId=bundleId, languages=[language]) # 4 - create a shortcut for the function _ = t.gettext # 5 - get the translated value from Globalization Pipeline service # by calling the shortcut translatedValue = _('welcome') messages.append({'language': language, 'welcome': translatedValue}) return render_template('index.html', messages=messages)