Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add gettext translation keys manually

Tags:

I'm creating a project with the phoenixframework and I use gettext to add some translations. The command mix gettext.extract --merge grabs all my translation keys from the controllers and templates and stores it in the .pot and .po files.

But some translation keys I have in a config file, which I use as select options in a form select box. At the moment I have a list of keys: [:key1, :key2, key3, ..., keyN].

Now I need a keyword list to display the translations and to select the keys with the select box. My result is this: [{"translation 1": key1}, {"translation 2": key2}, {"translation 3": key3}, ..., {"translation N": keyN}].

Problem:

I should not touch my .pot files and I can not write my translation keys into my .po files, because they will be lost after the run of the command above. The files will be overwritten.

Is there a way to add translations manually so that I can run the command to grab new translations from my project without to lose my manually added translations?

like image 434
guitarman Avatar asked May 24 '16 11:05

guitarman


1 Answers

There is a solution and it works with the elixir get text implementation out of the box.

The keyword is: domain

At first I created a new pot file with all of my translation keys:

msgid ""
msgstr ""
"Language: en\n"

msgid "key1"
msgstr ""

msgid "key2"
msgstr ""

# ... and so on ...

My file is called additionals.pot. Then I run mix gettext.extract --merge. Now I have an additionals.po file within each language directory, where I add my translations now.

Within my project now I use the dgettext method of the Gettext module instead of gettext. dgettext lets me specify a domain which is the basename of the new additionals.po file.

Gettext.dgettext(<MyProjectName>.Gettext, "additionals", "key1")
# => "translation 1"

That's it and everything works as expected.

Here is a nice article about Gettext. Search for Domains to find another description how dgettext is used.

like image 56
guitarman Avatar answered Oct 10 '22 23:10

guitarman