In previous topics we define messages directly in source code. But in real application you will place it in separate files. Let’s see how to do it.
Files listed below are available in the examples directory:
/ # Sample application root directory
translations/ # Translations directory
en-US/ # "en-US" locale
object.yaml # "object" catalog
message.yaml # "message" catalog
ru-RU/ # "ru-RU" locale
proto.yaml # "proto" catalog
object.yaml # "object" catalog
message.yaml # "message" catalog
helpers.py # Helpers module
app.py # Application itself
# translations/en-US/object.yaml
article: Article
comment: Comment
# translations/en-US/message.yaml
__helper__:
a: helpers:a
removed:
filter: |
$object = self.locale['object'][$object].format()
msg: "{object} has been successfully removed"
choose:
filter: |
$object = self.locale['object'][$object].format().lower()
$an = helper.a($object)
msg: "Please, choose {an} {object} for removal"
# translations/ru-RU/proto.yaml
noun:
defaults:
case: n
key: "{case}"
# translations/ru-RU/object.yaml
article:
prototype: proto:noun
msg:
n: Статья
a: Статью
gender: f
comment:
prototype: proto:noun
msg:
n: Комментарий
a: Комментарий
gender: m
# translations/ru-RU/message.yaml
removed:
filter: |
$object = self.locale['object'][$object]
$gender = $object.gender
$object = $object.format()
key: "{gender}"
msg:
m: "{object} успешно удален"
f: "{object} успешно удалена"
n: "{object} успешно удалено"
choose:
filter: |
$object = self.locale['object'][$object] \
.format(case='a').lower()
msg: "Выберете {object} для удаления"
# helpers.py
def a(lang, country):
if lang != 'en':
raise ValueError('Unsupported language "{0}"'.format(lang))
def helper_a(word):
if word[0].lower() in ('a', 'o', 'i', 'e'):
return 'an'
return 'a'
return helper_a
# 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', object='article') ==
u'Выберете статью для удаления')
assert(t.translate('message:choose', 'Choose an object', object='comment') ==
u'Выберете комментарий для удаления')
Take a notice to scan parameter passed to Translator. It replaced locales one. See h10n.source module doc-strings for details.