Skip to content

Instantly share code, notes, and snippets.

@shoorick shoorick/README
Created Dec 11, 2010

Embed
What would you like to do?
Gettext example with Locale::TextDomain
#!/usr/bin/perl -wl
use strict;
=head1 DESCRIPTION
gettext example
=cut
warn 'LANGUAGE: ', $ENV{'LANGUAGE'};
my $lang;
if ( $lang = shift @ARGV) {
$ENV{'LANGUAGE'} = $lang;
warn "lang: $lang";
}
use Locale::TextDomain ( 'example' , '../locale/' );
use POSIX;
setlocale( LC_MESSAGES, '' );
# Singular
print __ 'Example';
my $count = int rand 33;
# Plural
printf
__n(
"%d apple was found.\n",
"%d apples were found.\n",
$count
),
$count;
# Plural again
print __nx (
"{count} apple was found.\n",
"{count} apples were found.\n",
$count,
'count' => $count
);
=head1 AUTHOR
Alexander Sapozhnikov L<< http://shoorick.ru/ >>
=cut
Example if gettext usage via Locale::TextDomain
How to use:
0. cd po && make
1. ../example/1.pl
or
env LANGUAGE=ru ../example/1.pl
or
../example/1.pl de
etc
2. You can add new languages:
cd po
make up
cp example.pot TWO_LETTER_CODE_OF_LANGUAGE.po
# edit TWO_LETTER_CODE_OF_LANGUAGE.po
make
Alexander Sapozhnikov
http://shoorick.ru/
#, fuzzy
msgid ""
msgstr ""
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2010-12-11 01:56+0500\n"
"Last-Translator: Alexander Sapozhnikov <as@urc.ac.ru>\n"
"Language-Team: \n"
"Language: German\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n!=1;\n"
#: ../example/1.pl:23
msgid "Example"
msgstr "Beispiel"
#: ../example/1.pl:30
#, perl-format
msgid "%d apple was found.\n"
msgid_plural "%d apples were found.\n"
msgstr[0] "%d Apfel gefunden wurde.\n"
msgstr[1] "%d Äpfel gefunden wurde.\n"
#: ../example/1.pl:37
#, perl-brace-format
msgid "{count} apple was found.\n"
msgid_plural "{count} apples were found.\n"
msgstr[0] "{count} Apfel gefunden wurde.\n"
msgstr[1] "{count} Äpfel gefunden wurde.\n"
MO_PATH=../locale/
LOCALIZABLE=../example/*.pl
TEMPLATE=example.pot
.PHONY: compile update clean help
compile:
@for i in $$(ls *.po); do \
mkdir -p $(MO_PATH)$${i%.po}/LC_MESSAGES ; \
msgfmt $$i -o $(MO_PATH)$${i%.po}/LC_MESSAGES/example.mo; \
echo "$$i -> $(MO_PATH)$${i%.po}/LC_MESSAGES/example.mo"; \
done
up update: $(LOCALIZABLE)
@echo Gathering translations...
@xgettext -L Perl \
-k__ -k\$__ -k%__ -k__n:1,2 -k__nx:1,2 -k__np:2,3 -k__npx:2,3 -k__p:2 \
-k__px:2 -k__x -k__xn:1,2 -kN__ -kN__n -kN__np -kN__p -k \
--from-code utf-8 -o $(TEMPLATE) $(LOCALIZABLE)
@echo Merging...
@for i in $$(ls *.po); do \
cp $$i $$i~; \
echo -n "$$i "; \
msgmerge $$i~ $(TEMPLATE) > $$i; \
done
@echo
clean:
@rm -f *~
help:
@echo 'Available goals: compile, update, clean, help.'
msgid ""
msgstr ""
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2010-12-11 01:56+0500\n"
"Last-Translator: Alexander Sapozhnikov <as@urc.ac.ru>\n"
"Language-Team: \n"
"Language: Russian\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;"
#: ../example/1.pl:23
msgid "Example"
msgstr "Пример"
#: ../example/1.pl:30
#, perl-format
msgid "%d apple was found.\n"
msgid_plural "%d apples were found.\n"
msgstr[0] "%d яблоко нашлось.\n"
msgstr[1] "%d яблока нашлось.\n"
msgstr[2] "%d яблок нашлось.\n"
#: ../example/1.pl:37
#, perl-brace-format
msgid "{count} apple was found.\n"
msgid_plural "{count} apples were found.\n"
msgstr[0] "{count} яблоко нашлось.\n"
msgstr[1] "{count} яблока нашлось.\n"
msgstr[2] "{count} яблок нашлось.\n"
@shoorick

This comment has been minimized.

Copy link
Owner Author

commented Dec 11, 2010

Пример применения библиотеки gettext в программах на перле с использованием модуля Locale::TextDomain

Структура каталогов:

  • example — скрипты.
  • locale — скомпилированные mo-файлы переводов.
    • de — немецкие
    • ru — русские
      • LC_MESSAGES — сами mo-файлы.
    • ЯЗ — для языка с кодом ЯЗ
  • po — исходные po-файлы переводов и Makefile.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.