from logging import basicConfig, DEBUG from h10n import Translator basicConfig(level=DEBUG) locales = { 'en-US': { 'test': { 'error': { 'msg': '...', 'filter': 'raise Exception("Raised from filter")', } } }, 'en-GB': { 'test': {}, }, 'ru-RU': {}, } t = Translator(locales=locales, default='en-US', fallback={'ru-RU': 'en-GB'}) t.locale = 'ru-RU' assert (t.translate('test:error', 'Error') == 'Error')
} } }, 'object': { 'article': { 'msg': u'Статья', 'gender': 'f', }, 'comment': { 'msg': u'Комментарий', 'gender': 'm', }, }, }, } t = Translator(locales=locales, default='en-US') assert (t.translate( 'message:removed', 'Object has been removed', object='article') == u'Article has been successfully removed') assert (t.translate( 'message:removed', 'Object has been removed', object='comment') == u'Comment has been successfully removed') t.lang = 'ru' assert (t.translate('message:removed', 'Object has been removed', object='article') == u'Статья успешно удалена') assert (t.translate('message:removed', 'Object has been removed', object='comment') == u'Комментарий успешно удален')
'm': u'{object} успешно удален', 'f': u'{object} успешно удалена', 'n': u'{object} успешно удалено', } } }, 'object': { 'article': { 'msg': u'Статья', 'gender': 'f', }, 'comment': { 'msg': u'Комментарий', 'gender': 'm', }, }, }, } t = Translator(locales=locales, default='en-US') assert(t.translate('message:choose', 'Choose an object', object='article') == u'Please, choose an article for removal') assert(t.translate('message:choose', 'Choose an object', object='comment') == u'Please, choose a comment for removal') t.lang = 'ru' assert(t.translate('message:choose', 'Choose an object', object='article') == u'Please, choose an article for removal') assert(t.translate('message:choose', 'Choose an object', object='comment') == u'Please, choose a comment for removal')
# coding: utf-8 from h10n import Translator locales = { 'en-US': { 'object': { 'article': u'Article', 'comment': u'Comment', }, }, 'ru-RU': { 'object': { 'article': u'Статья', 'comment': u'Комментарий', }, }, } t = Translator(locales=locales, default='en-US') assert (t.translate('object:article', u'Article') == u'Article') assert (t.translate('object:comment', u'Comment') == u'Comment') t.lang = 'ru' assert (t.translate('object:article', u'Article') == u'Статья') assert (t.translate('object:comment', u'Comment') == u'Комментарий')
from h10n import Translator locales = { 'en-US': { 'object': { 'article': u'Article', 'comment': u'Comment', }, }, } t = Translator(locales=locales) assert (t.translate('object:article', u'Article') == u'Article') assert (t.translate('object:comment', u'Comment') == u'Comment')
# app.py # coding: utf-8 import os from h10n import Translator path = os.path.join(os.path.dirname(__file__), 'translations') t = Translator(scan='path://' + path, default='en-US') assert (t.translate( 'message:removed', 'Object has been removed', object='article') == u'Article has been successfully removed') assert (t.translate( 'message:removed', 'Object has been removed', object='comment') == u'Comment has been successfully removed') assert (t.translate( 'message:choose', 'Choose an object', object='article') == u'Please, choose an article for removal') assert (t.translate( 'message:choose', 'Choose an object', object='comment') == u'Please, choose a comment for removal') t.lang = 'ru' assert (t.translate('message:removed', 'Object has been removed', object='article') == u'Статья успешно удалена') assert (t.translate('message:removed', 'Object has been removed', object='comment') == u'Комментарий успешно удален') assert (t.translate('message:choose', 'Choose an object',
'gender': 'f', }, 'comment': { 'defaults': { 'case': 'n' }, 'key': '{case}', 'msg': { 'n': u'Комментарий', 'a': u'Комментарий', }, 'gender': 'm', }, }, }, } t = Translator(locales=locales, default='en-US') assert (t.translate( 'message:choose', 'Choose an object', object='article') == u'Please, choose an article for removal') assert (t.translate( 'message:choose', 'Choose an object', object='comment') == u'Please, choose a comment for removal') t.lang = 'ru' assert (t.translate('message:choose', 'Choose an object', object='article') == u'Выберете статью для удаления') assert (t.translate('message:choose', 'Choose an object', object='comment') == u'Выберете комментарий для удаления')