|
NAMELocale::Maketext::Gettext - Joins the gettext and Maketext frameworksSYNOPSISIn your localization class:package MyPackage::L10N; use base qw(Locale::Maketext::Gettext); return 1; In your application: use MyPackage::L10N; $LH = MyPackage::L10N->get_handle or die "What language?"; $LH->bindtextdomain("mypackage", "/home/user/locale"); $LH->textdomain("mypackage"); $LH->maketext("Hello, world!!"); If you want to have more control to the detail: # Change the output encoding $LH->encoding("UTF-8"); # Stick with the Maketext behavior on lookup failures $LH->die_for_lookup_failures(1); # Flush the MO file cache and re-read your updated MO files $LH->reload_text; # Set the encoding of your maketext keys, if not in English $LH->key_encoding("Big5"); # Set the action when encode fails $LH->encode_failure(Encode::FB_HTMLCREF); Use Locale::Maketext::Gettext to read and parse the MO file: use Locale::Maketext::Gettext; %Lexicon = read_mo($mo_file); DESCRIPTIONLocale::Maketext::Gettext joins the GNU gettext and Maketext frameworks. It is a subclass of Locale::Maketext(3) that follows the way GNU gettext works. It works seamlessly, both in the sense of GNU gettext and Maketext. As a result, you enjoy both their advantages, and get rid of both their problems, too.You start as a usual GNU gettext localization project: Work on PO files with the help of translators, reviewers and Emacs. Turn them into MO files with msgfmt. Copy them into the appropriate locale directory, such as /usr/share/locale/de/LC_MESSAGES/myapp.mo. Then, build your Maketext localization class, with your base class changed from Locale::Maketext(3) to Locale::Maketext::Gettext. That is all. METHODS
FUNCTIONS
NOTESWARNING: do not try to put any lexicon in your language subclass. When the "textdomain" method is called, the current lexicon will be replaced, but not appended. This is to accommodate the way "textdomain" works. Messages from the previous text domain should not stay in the current text domain.An essential benefit of this Locale::Maketext::Gettext over the original Locale::Maketext(3) is that: GNU gettext is multibyte safe, but Perl source is not. GNU gettext is safe to Big5 characters like \xa5\x5c (Gong1). But if you follow the current Locale::Maketext(3) document and put your lexicon as a hash in the source of a localization subclass, you have to escape bytes like \x5c, \x40, \x5b, etc., in the middle of some natural multibyte characters. This breaks these characters in halves. Your non-technical translators and reviewers will be presented with unreadable mess, "Luan4Ma3". Sorry to say this, but it is weird for a localization framework to be not multibyte-safe. But, well, here comes Locale::Maketext::Gettext to rescue. With Locale::Maketext::Gettext, you can sit back and relax now, leaving all this mess to the excellent GNU gettext framework. The idea of Locale::Maketext::Gettext came from Locale::Maketext::Lexicon(3), a great work by Autrijus. But it has several problems at that time (version 0.16). I was first trying to write a wrapper to fix it, but finally I dropped it and decided to make a solution towards Locale::Maketext(3) itself. Locale::Maketext::Lexicon(3) should be fine now if you obtain a version newer than 0.16. Locale::Maketext::Gettext also solved the problem of lack of the ability to handle the encoding in Locale::Maketext(3). I implement this since this is what GNU gettext does. When %Lexicon is read from MO files by "read_mo()", the encoding tagged in gettext MO files is used to "decode" the text into the internal encoding of Perl. Then, when extracted by "maketext", it is "encode"d by the current "encoding" value. The "encoding" can be set at run time, so that you can run a daemon and output to different encoding according to the language settings of individual users, without having to restart the application. This is an improvement to the Locale::Maketext(3), and is essential to daemons and "mod_perl" applications. You should trust the encoding of your gettext MO file. GNU gettext "msgfmt" checks the illegal characters for you when you compile your MO file from your PO file. The encoding form your MO files are always good. If you try to output to a wrong encoding, part of your text may be lost, as "FB_DEFAULT" does. If you do not like this "FB_DEFAULT", change the failure behavior with the method "encode_failure". If you need the behavior of auto Traditional Chinese/Simplified Chinese conversion, as GNU gettext smartly does, do it yourself with Encode::HanExtra(3), too. There may be a solution for this in the future, but not now. If you set "textdomain" to a domain that is not "bindtextdomain" to specific a locale directory yet, it will try search system locale directories. The current system locale directory search order is: /usr/share/locale, /usr/lib/locale, /usr/local/share/locale, /usr/local/lib/locale. Suggestions for this search order are welcome. NOTICE: MyPackage::L10N::en->maketext(...) is not available anymore, as the "maketext" method is no more static. That is a sure result, as %Lexicon is imported from foreign sources dynamically, but not statically hardcoded in Perl sources. But the documentation of Locale::Maketext(3) does not say that you can use it as a static method anyway. Maybe you were practicing this before. You had better check your existing code for this. If you try to invoke it statically, it returns "undef". "dgettext" and "dcgettext" in GNU gettext are not implemented. It is not possible to temporarily change the current text domain in the current design of Locale::Maketext::Gettext. Besides, it is meaningless. Locale::Maketext is object-oriented. You can always raise a new language handle for another text domain. This is different from the situation of GNU gettext. Also, the category is always "LC_MESSAGES". Of course it is. We are gettext and Maketext. Avoid creating different language handles with different textdomain on the same localization subclass. This currently works, but it violates the basic design of Locale::Maketext(3). In Locale::Maketext(3), %Lexicon is saved as a class variable, in order for the lexicon inheritance system to work. So, multiple language handles to a same localization subclass shares a same lexicon space. Their lexicon space clash. I tried to avoid this problem by saving a copy of the current lexicon as an instance variable, and replacing the class lexicon with the current instance lexicon whenever it is changed by another language handle instance. But this involves large scaled memory copy, which affects the performance seriously. This is discouraged. You are advised to use a single textdomain for a single localization class. The "key_encoding" is a workaround, not a solution. There is no solution to this problem yet. You should avoid using non-English language as your original text. You will get yourself into trouble if you mix several original text encodings, for example, joining several pieces of code from programmers all around the world, with their messages written in their own language and encodings. Solution suggestions are welcome. "pgettext" in GNU gettext is implemented as "pmaketext", in order to look up the text message translation in a particular context. Thanks to the suggestion from Chris Travers. BUGSGNU gettext never fails. I tries to achieve it as long as possible. The only reason that maketext may die unexpectedly now is "Unterminated bracket group". I cannot get a better solution to it currently. Suggestions are welcome.You are welcome to fix my English. I have done my best to this documentation, but I am not a native English speaker after all. ^^; SEE ALSOLocale::Maketext(3), Locale::Maketext::TPJ13(3), Locale::Maketext::Lexicon(3), Encode(3), bindtextdomain(3), textdomain(3). Also, please refer to the official GNU gettext manual at <https://www.gnu.org/software/gettext/manual/>.AUTHORimacat <imacat@mail.imacat.idv.tw>COPYRIGHTCopyright (c) 2003-2021 imacat. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |