Template::Plugin::Gettext(3pm) | User Contributed Perl Documentation | Template::Plugin::Gettext(3pm) |
Template::Plugin::Gettext - Gettext Support For the Template Toolkit Version 2
Load the plug-in in templates:
[% USE Gettext('com.textdomain.my', 'fr', 'utf-8', 'DIRECTORIES'...) %] [% Gettext.gettext('Hello, world!') %] [% 'Hello, world!' | gettext %]
Or alias "Gettext":
[% USE gtx = Gettext('com.textdomain.my', 'fr', 'utf-8', 'DIRECTORIES'...) %] [% gtx.gettext('Hello, world!') %]
Use method invocations:
[% Gettext.gettext("Hello, world!") %] [% Gettext.xgettext("Hello, {name}!", name => 'John Doe') %]
Or filters (without the prefix):
[% FILTER gettext %] Hello, world! [% END %] [% 'Hello, world!' | gettext %] [% FILTER xgettext(name => 'John Doe') %] Hello, {name}! [% END %]
You have a multitude of methods available:
[% gtx.gettext("Hello, user!") %] [% gtx.xgettext("Hello, {user}!", user => 'John Doe') %] [% gtx.ngettext("One document deleted.", "Multiple documents deleted."), 42) %] [% gtx.nxgettext("One document deleted.", "{num} documents deleted."), 42, num => 42) %] [% gtx.npgettext("context..." "One document deleted.", "Multiple documents deleted."), 42) %] [% gtx.npxgettext("context...", "One document deleted.", "{num} documents deleted."), 42, num => 42) %]
The Template::Plugin::Gettext plug-in makes the GNU gettext API <https://www.gnu.org/software/gettext/> available for documents using the Template Toolkit version 2 <http://template-toolkit.org/>. See <https://github.com/gflohr/Template-Plugin-Gettext> for an overall picture and the recommended tool-chain.
The following methods produce translatable content:
[% gtx.xgettext("String with {LBRACE}PLACEHOLDERS{RBRACE}", LBRACE => "{", RBRACE => "}") %]
[% gtx.gettext("State: ") %] [% gtx.gettext("Open")] | [% gtx.gettext("Close") %] [% gtx.gettext("Menu:") %] [% gtx.pgettext("menu", "Open")] [% gtx.gettext("Save")] [% gtx.gettext("Save As")] [% gtx.pgettext("menu", "Close")]
The strings "Open" and "Close" in line 2
are adjectives. As
menu entries they are verb forms and will have a different
translation in many languages.
In doubt: Only use contexts if one of your translators complains about a message having multiple meanings.
Example:
[% gtx.nxgettext("One document deleted", "{num} documents deleted"), count, num => count) %]
In English this will produce "42 documents deleted" if the variable count has the value 42. It will produce "One document deleted" if the variable count has the value 1.
In other languages, the rules for plural forms may be a lot simpler (for example Chinese, which has no plural) or a lot more complicated (for example Russian with two or Slovenian with even 3 plural forms). Using ngettext() gives your translators the chance to provide syntactically correct translations for these cases.
The function is a mixture of xgettext(), ngettext(), and pgettext(), see above!
In fact, you have also all the keywords used for "FILTERS" available but those not listed here have such an odd ordering of arguments that they are not listed here.
The entire gettext API is also exposed as a filter. There are two things to note here:
[% FILTER gettextp("greeting") %] Hello, world! [% END %] or 100 % equivalent: [% 'Hello, world!' | gettextp("greeting") %]
Why? The text between FILTER and END resp. the text in front of the pipe symbol | is always the first argument. This plug-in therefore tries to make the first argument the most significant one. Nobody stops you from writing the following:
[% FILTER pgettext("Hello, world!") %] greeting [% END %] or again 100 % equivalent: [% 'greeting' | pgettext("Hello, world!") %]
It produces exactly the same results as above but it looks a little bit odd, doesn't it?
It would have been arguably better understandable to silently reorder the arguments, when using the plug-in as a filter. But it would break extraction of your strings with xgettext-tt2 (Locale::XGettext::TT2) because the string extractor would then confuse the arguments.
But stay relaxed! Message contexts are rarely needed, and when you need them, you have to live with this little weirdness.
In order to avoid confusion, those filters that would not have the translatable string (in the singular form) where one would expect it, are not documented here.
You can use the following filters:
[% "String with {LBRACE}PLACEHOLDERS{RBRACE}" | xgettext(LBRACE => "{", RBRACE => "}") %]
Example:
[% "One document deleted" | nxgettext("{num} documents deleted"), count, num => count) %]
In English this will produce "42 documents deleted" if the variable count has the value 42. It will produce "One document deleted" if the variable count has the value 1.
In other languages, the rules for plural forms may be a lot simpler (for example Chinese, which has no plural) or a lot more complicated (for example Russian with two or Slovenian with even 3 plural forms). Using ngettext() gives your translators the chance to provide syntactically correct translations for these cases.
The filter is a mixture of xgettext(), ngettext(), and pgettext(), see above!
[% USE gtx = Gettext('com.mydomain.www', de') %] Using locale [% debug_locale() %].
This way, you can determine whether setting the specified locale actually worked.
The purpose of this method is to allow harvesting files that should be processed by xgettext-tt2.
If the template is either "input text" or "input file handle", the template variable "gettext_filename" - if existing - is assumed as the template name. Rationale: "input text" and "input file handle" are used by the Template Toolkit as aliases, when reading from a scalar or a file handle.
Copyright (C) 2016-2018 Guido Flohr (http://www.guido-flohr.net/). License LGPLv3+: GNU General Public License version 3 or later <http://gnu.org/licenses/lgpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>, all rights reserved.
Template, Template::Manual::Filters, xgettext-tt2, perl
2023-03-18 | perl v5.36.0 |