Este é um post de José Lopes.
Este post explica como criar para plugins do Wordpress o ficheiro linguístico base (.POT) e os ficheiros
específicos para cada língua (.PO e .MO), por modo a internacionalizar os plugins.
O Wordpress explica no seu site como criar estes ficheiros mas eu acho a sua explicação um pouco confusa
e espalhada por vários links, pelo que espero a concentrar neste post.
Posted by José Lopes.
This post explains how to create for Wordpress plugins the language base file (.POT) and the specific files
for each language version (.PO and .MO), in order to provide the plugins internationalization.
The Wordpress already explains how to create them but I feel that the explanation is not straight forward
and its spread by to many links, so I hope have it all here.
Partindo do princípio que escreveu o seu plugin e que pretende o internacionalizar, deve ter
antes do mais:
- No topo do ficheiro PHP principal do plugin, i.e., antes de começar as funções do plugin, o seguinte código deve
ser inserido:
if(function_exists('load_plugin_textdomain'))
load_plugin_textdomain('NOME PLUGIN','wp-content/plugins/DIRECTORIA PLUGIN');
- Os pedaços de textos sujeitos a tradução devem estar na forma:
__("TEXTO A TRADUZIR","NOME PLUGIN")
Estes dois detalhes são cruciais para se poder gerar os ficheiros linguísticos e para que o Wordpress
encontre automaticamente a versão correcta de acordo com as definições do utilizador.
Começamos então por gerar o ficheiro .POT, bastando escrever numa shell de um sistema Linux:
xgettext NOME PLUGIN.pot
Isto cria o ficheiro .POT que tem o aspecto:
# translation of hideshowcomments.po to
# Jose Lopes <jose.lopes@paxjula.com>, 2007.
msgid ""
msgstr ""
"Project-Id-Version: hideshowcomments\n"
"POT-Creation-Date: 2007-09-18 10:42+0100\n"
"PO-Revision-Date: 2007-09-18 02:33+0200\n"
"Last-Translator: Jose Lopes <jose.lopes@paxjula.com>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-KeywordsList: __;_e\n"
"X-Generator: KBabel 1.10.2\n"
#: hideshowcomments.php:42
msgid "Click here to see or add comments"
msgstr ""
Como podem ver nas linhas finais, este ficheiro vai ter para cada linha do plugin onde definimos o texto a traduzir,
neste exemplo para o plugin
hideshowcomments,
a versão em inglês em msgid (como default) e o espaço para a versão da nova língua em msgstr.
Para criar uma versão para uma língua deve-se partir deste ficheiro, editando-o por exemplo no KBabel,
e gravando-o com o nome:
NOME PLUGIN-CÓDIGO LÍNGUA_PAÍS.po
Deve-se utilizar os standards para definir língua_país (exemplo: pt_PT para português de Portugal e pt_BR para português do Brasil).
Para mais informação sobre os códigos língua_país ver os links:
Uma vez feita a tradução podemos criar o ficheiro .MO, bastando para isso escrever numa shell:
msgfmt -o NOME PLUGIN-CÓDIGO LÍNGUA_PAÍS.mo NOME PLUGIN-CÓDIGO LÍNGUA_PAÍS.po
Numa distribuição de Plugin devemos facultar os ficheiros .MO de cada língua juntamente com o ficheiro .POT,
para permitir que alguém traduza para outra língua não disponibilizada.
Assuming that you wrote your plugin and you want to have it internationalized, you must have before
anything:
- At the top of the plugin main PHP file, i.e., before any other plugin function, the following code:
if(function_exists('load_plugin_textdomain'))
load_plugin_textdomain('PLUGIN NAME','wp-content/plugins/PLUGIN DIRECTORY');
- The text to be translated has to be like:
__("TEXT TO TRANSLATE","PLUGIN NAME")
These two details are crucial in order to generate the language files and to help Wordpress finding automatically the correct version
in accordance with the user definitions.
To generate the .POT file, using a Linux system, just write on a shell:
xgettext PLUGIN NAME.pot
The .POT file created has the following look:
# translation of hideshowcomments.po to
# Jose Lopes <jose.lopes@paxjula.com>, 2007.
msgid ""
msgstr ""
"Project-Id-Version: hideshowcomments\n"
"POT-Creation-Date: 2007-09-18 10:42+0100\n"
"PO-Revision-Date: 2007-09-18 02:33+0200\n"
"Last-Translator: Jose Lopes <jose.lopes@paxjula.com>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-KeywordsList: __;_e\n"
"X-Generator: KBabel 1.10.2\n"
#: hideshowcomments.php:42
msgid "Click here to see or add comments"
msgstr ""
Has you can see on the final lines, this file will have for each line where we set the text for translation,
in this case for the
hideshowcomments
plugin, the default english version at msgid and the space for the new language version at msgstr.
To create a new language version you start from this file, edit it with KBabel for instance, and rename it like:
PLUGIN NAME-LOCAL CODE LANGUAGE_COUNTRY.po
We should use the standards for the language_country codes (as example: pt_PT for portuguese of Portugal and pt_BR
for portuguese of Brasil).
For more information about the language_country codes check:
Once translated we can create the .MO file, writing on a shell:
msgfmt -o PLUGIN NAME-LOCAL CODE LANGUAGE_COUNTRY.mo PLUGIN NAME-LOCAL CODE LANGUAGE_COUNTRY.po
While distributing the plugin the .MO file of each language should be included as well as the .POT file, allowing
others to translate into other languages.