{
    "mode": "perldoc",
    "parameter": "Locale::Maketext::Cookbook",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Locale%3A%3AMaketext%3A%3ACookbook/json",
    "generated": "2026-06-03T06:44:55Z",
    "sections": {
        "NAME": {
            "content": "Locale::Maketext::Cookbook - recipes for using Locale::Maketext\n",
            "subsections": []
        },
        "INTRODUCTION": {
            "content": "This is a work in progress. Not much progress by now :-)\n",
            "subsections": []
        },
        "ONESIDED LEXICONS": {
            "content": "*Adapted from a suggestion by Dan Muey*\n\nIt may be common (for example at your main lexicon) that the hash keys and values coincide. Like\nthat\n\nq{Hello, tell me your name}\n=> q{Hello, tell me your name}\n\nIt would be nice to just write:\n\nq{Hello, tell me your name} => ''\n\nand have this magically inflated to the first form. Among the advantages of such representation,\nthat would lead to smaller files, less prone to mistyping or mispasting, and handy to someone\ntranslating it which can simply copy the main lexicon and enter the translation instead of\nhaving to remove the value first.\n\nThat can be achieved by overriding \"init\" in your class and working on the main lexicon with\ncode like that:\n\npackage My::I18N;\n...\n\nsub init {\nmy $lh = shift; # a newborn handle\n$lh->SUPER::init();\ninflatelexicon(\\%My::I18N::en::Lexicon);\nreturn;\n}\n\nsub inflatelexicon {\nmy $lex = shift;\nwhile (my ($k, $v) = each %$lex) {\n$v = $k if !defined $v || $v eq '';\n}\n}\n\nHere we are assuming \"My::I18N::en\" to own the main lexicon.\n\nThere are some downsides here: the size economy will not stand at runtime after this \"init()\"\nruns. But it should not be that critical, since if you don't have space for that, you won't have\nspace for any other language besides the main one as well. You could do that too with ties,\nexpanding the value at lookup time which should be more time expensive as an option.\n",
            "subsections": []
        },
        "DECIMAL PLACES IN NUMBER FORMATTING": {
            "content": "*After CPAN RT #36136 (<https://rt.cpan.org/Ticket/Display.html?id=36136>)*\n\nThe documentation of Locale::Maketext advises that the standard bracket method \"numf\" is limited\nand that you must override that for better results. It even suggests the use of Number::Format.\n\nOne such defect of standard \"numf\" is to not be able to use a certain decimal precision. For\nexample,\n\n$lh->maketext('pi is [numf,1]', 355/113);\n\noutputs\n\npi is 3.14159292035398\n\nSince pi ≈ 355/116 is only accurate to 6 decimal places, you would want to say:\n\n$lh->maketext('pi is [numf,1,6]', 355/113);\n\nand get \"pi is 3.141592\".\n\nOne solution for that could use \"Number::Format\" like that:\n\npackage Wuu;\n\nuse base qw(Locale::Maketext);\n\nuse Number::Format;\n\n# can be overridden according to language conventions\nsub numfparams {\nreturn (\n-thousandssep  => '.',\n-decimalpoint  => ',',\n-decimaldigits => 2,\n);\n}\n\n# builds a Number::Format\nsub numfformatter {\nmy ($lh, $scale) = @;\nmy @params = $lh->numfparams;\nif ($scale) { # use explicit scale rather than default\npush @params, (-decimaldigits => $scale);\n}\nreturn Number::Format->new(@params);\n}\n\nsub numf {\nmy ($lh, $n, $scale) = @;\n# get the (cached) formatter\nmy $nf = $lh->{nf}{$scale} ||= $lh->numfformatter($scale);\n# format the number itself\nreturn $nf->formatnumber($n);\n}\n\npackage Wuu::pt;\n\nuse base qw(Wuu);\n\nand then\n\nmy $lh = Wuu->gethandle('pt');\n$lh->maketext('A [numf,1,3] km de distância', 1550.2222);\n\nwould return \"A 1.550,222 km de distância\".\n\nNotice that the standard utility methods of \"Locale::Maketext\" are irremediably limited because\nthey could not aim to do everything that could be expected from them in different languages,\ncultures and applications. So extending \"numf\", \"quant\", and \"sprintf\" is natural as soon as\nyour needs exceed what the standard ones do.\n",
            "subsections": []
        }
    },
    "summary": "Locale::Maketext::Cookbook - recipes for using Locale::Maketext",
    "flags": [],
    "examples": [],
    "see_also": []
}