njfranck has asked for the wisdom of the Perl Monks concerning the following question:

In Locale::Maketext::Lexicon, when you request a handle for a language that is not configured, it falls back to language "en", when it is available in the configuration.
package Loc; use parent "Locale::Maketext"; use Locale::Maketext::Lexicon { en => [ Gettext => \*main::DATA ] }; package main; use strict; print ( Loc->get_handle("nl") ? "ERROR: should be undef" : "OK: got un +def" ); print "\n"; __DATA__ msgid "greet" msgstr "hello %1"
See? I request language "nl", but I get the handle for "en". The opposite works as expected (at least as I expect it):
package Loc; use parent "Locale::Maketext"; use Locale::Maketext::Lexicon { nl => [ Gettext => \*main::DATA ] }; package main; use strict; print ( Loc->get_handle("en") ? "ERROR: should be undef" : "OK: got un +def" ); print "\n"; __DATA__ msgid "greet" msgstr "hello %1"
Any idea of how to disable this fallback behaviour? I read something about the method "language_languages" in Locale::Maketext, but that does not make any difference.

Replies are listed 'Best First'.
Re: Disable use of fallback language in Locale::Maketext::Lexicon
by Eily (Monsignor) on Oct 09, 2018 at 13:59 UTC

    Hello njfranck. I suppose you meant "fallback_languages" rather than "language_languages"? If so, overwriting it actually (partially) works: the list of default it returns is ('i-default', 'en', 'en-US'), and both "i-default" and "en-US" are successfully removed as fallbacks when you have a Loc::fallback_languages defined.

    But beside those fallbacks, Locale::Maketext also uses the panic languages from I18N::LangTags as seen here.

    One way to disable that behaviour is to set $Locale::Maketext::USING_LANGUAGE_TAGS = 0;. I can't tell you what else it does though (it's not documented and I don't want to analyze the whole code).

    Another way to it is to locally replace I18N::LangTags::panic_languages so that it returns the empty list. It's a little dirtier, but at least it's limited in scope:

    package Loc; use parent "Locale::Maketext"; use Locale::Maketext::Lexicon { "en" => [ Gettext => \*main::DATA ] }; sub fallback_languages { }; package main; use Data::Dump qw( pp ); use strict; { # The effect of local will be reversed outside of this block local *I18N::LangTags::panic_languages = sub {}; print ( Loc->get_handle("nl") ? pp(Loc->get_handle("nl")) : "OK: got + undef" ); } # end of local, so limited side effects print "\n"; __DATA__ msgid "greet" msgstr "hello %1"
    Notice that I still have Loc::fallback_languages to overwrite one set of defaults beside the panic languages ("en" is defined in both)

      Thanks for your reply! I'll have a look at it.