Search code examples
perllocalizationlocalepo

Localization via Locale::Maketext::Simple always falls back to default instead of .po entry


In a perl Module I want to use https://metacpan.org/pod/Locale::Maketext::Simple to convert strings to different languages.

My .po files are located unter /opt/x/languages, e.g. /opt/x/languages/en.po.

In my module I'm using the following header:

use Locale::Maketext::Simple (
    Path => '/opt/x/languages',
    Style => 'maketext'
);
loc_lang('en');

An entry in the .po files looks like this:

msgid "string [_1] to be converted"
msgstr "string [_1] is converted"

and the check via console with msgfmt -c en.po throws no errors so far.

But when I'm converting a string with loc() like loc("string [_1] to be converted", "xy") it gives me the output of "string xy to be converted" instead of "string xy is converted" as I would expect it. This looks to me like the .po files are not loaded correctly.

How can I check what .po files are found during maketext instantiation? Or am I mixing things up and there' a general mistake?


Edit 1: Thanks for the comments, but it still does not work.

I've checked the files with https://poedit.net/ and created the corresponding .mo files (currently for de and en) with this tool as well. They are located next to the .po files (inside /opt/x/languages).

For completeness, my header looks like this:

# MY OWN LANGUAGE FILE (DE)
# 06-2019 by me
#
msgid ""
msgstr ""
"Project-Id-Version: 1.0.0\n"
"POT-Creation-Date: 2019-06-01 00:00+0100\n"
"PO-Revision-Date: 2019-06-02 00:00+0100\n"
"Last-Translator: thatsme <[email protected]>\n"
"Language-Team: unknown\n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.2.3\n"

msgid "string [_1] to be converted"
msgstr "string [_1] is converted"

Solution

  • After some more digging and testing I finally found the issue for this behaviour, so here's my solution so far. Hope this may help others, because you rarely find any good documentation on this topic:

    Add libraries

    I added the library https://metacpan.org/pod/Locale::Maketext::Simple as stated above, but forgot to add https://metacpan.org/pod/Locale::Maketext::Lexicon.

    This took me quite long to see, because there were no exceptions or errors thrown, just... nothing.

    In the Maketext::Simple documentation it says

    If Locale::Maketext::Lexicon is not present, it implements a minimal localization function by simply interpolating [_1] with the first argument, [_2] with the second, etc.

    what looks at a first glance that .po files are loaded without Maketext::Lexikon, but it simply replaces placeholders.

    Other issues:

    I then discovered that all string are translated, except for the ones with placeholders like [_1]. I could not find a reason for this, but I moved to

    Style => 'gettext'
    

    and replaced all [_1], [_2]... with %1, %2... - that works like a charm.