uralbash
uralbash

Reputation: 3618

Pyramid Internationalization i18n word in database?

Is it possible to translate the words in the database? I want to use the standard tools: python setup.py extract_messages and use file locale/fr/LC_MESSAGES/*.po for translate.

Upvotes: 1

Views: 365

Answers (2)

Anouar Mokhtari
Anouar Mokhtari

Reputation: 2183

Yes, it's possible. You should implement a script to add the fields that you must translate at the end of the line in the file locale/project_name.pot

for exemple:


pyramid_i18n_howto/views.py:7
msgid "My i18n project"    #field_id to add in template 
msgstr ""                  # label to show

Default: Search documentation
pyramid_i18n_howto/templates/mytemplate.pt:37
msgid "search_documentation" 
msgstr ""

You'll also add it for each language in the file locale/it/project_name.po

see this for details

Upvotes: 0

Loïc Faure-Lacroix
Loïc Faure-Lacroix

Reputation: 13610

It is possible. Just pass the text returned from the database to a TranslationString.

You can use TranslationStringFactory(domain) to build your string and pass it to a localizer. The problem you might have is extracting strings from database. I'm not sure how you can do it but you might be able to pass an extractor to extract messages from database.

The other way which might be a bit better is to create a dummy file with strings. Extract all to a file formatted like gettext.

You can add it to revision and the current message extractors will get the text like usual

edit

Since how things are stored in database is always pretty custom, there is no such tool as far as I can tell. But it should be easy to write.

It should look like this.

with open(registry.settings['db_locale_file'], 'w') as f:
    for result in my_query_results:
        f.writeline('_(u"%s")' % result.key_you_want)

That's a pretty simple example but it should be more complicated. You can make it a script using pyramid bootstrap and then call

your_script
python setup.py extract_messages
python setup.py update_catalog
????
python setup.py compile_catalog
????
#profit!

Upvotes: 2

Related Questions