Python-Ref > GUI programming with PyGTK > Localization using gettext > Plural forms
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Plural forms

Expand/Shrink
  1   import gettext
  2   # arguments to install
  3   #  - domain
  4   #  - where to search for mo files
  5   #  - whether to convert translation strings to unicode
  6   #  - names - which additional functions should be installed
  7   gettext.install( "my_example_plural", "./", False, names=("ngettext",))
  8   print _ # this function is installed by gettext
  9   print ngettext
 10   print _( "Hello world.")
 11   print ngettext("I have %d apple.", "I have %d apples.", 1)
 12   print ngettext("I have %d apple.", "I have %d apples.", 1) % 1
 13   print ngettext("I have %d apple.", "I have %d apples.", 2)
 14   print ngettext("I have %d apple.", "I have %d apples.", 10)
 15   print ngettext("I have %d apple.", "I have %d apples.", 10) % 10
stdout:
<bound method NullTranslations.gettext of <gettext.NullTranslations instance at 0x7facfc99acb0>>
<bound method NullTranslations.ngettext of <gettext.NullTranslations instance at 0x7facfc99acb0>>
Hello world.
I have %d apple.
I have 1 apple.
I have %d apples.
I have %d apples.
I have 10 apples.
Doba běhu: 26.4 ms
Expand/Shrink
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: 3-1\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2010-03-10 19:17+0100\n"
"PO-Revision-Date: 2010-03-10 19:21+0100\n"
"Last-Translator: Beda Kosata <kosatab@vscht.cz>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;\n"

#: localization3-1.py:10
msgid "Hello world."
msgstr "Ahoj světe."

#: localization3-1.py:11 localization3-1.py:12 localization3-1.py:13
#: localization3-1.py:14
#, python-format
msgid "I have %d apple."
msgid_plural "I have %d apples."
msgstr[0] "Mám %d jabko."
msgstr[1] "Mám %d jabka."
msgstr[2] "Mám %d jablek."
Argumenty příkazové řádky:
LC_ALL=cs python localization3-1.py 
  1   import gettext
  2   # arguments to install
  3   #  - domain
  4   #  - where to search for mo files
  5   #  - whether to convert translation strings to unicode
  6   #  - names - which additional functions should be installed
  7   gettext.install( "my_example_plural", "./", False, names=("ngettext",))
  8   print _ # this function is installed by gettext
  9   print ngettext
 10   print _( "Hello world.")
 11   print ngettext("I have %d apple.", "I have %d apples.", 1)
 12   print ngettext("I have %d apple.", "I have %d apples.", 1) % 1
 13   print ngettext("I have %d apple.", "I have %d apples.", 2)
 14   print ngettext("I have %d apple.", "I have %d apples.", 10)
 15   print ngettext("I have %d apple.", "I have %d apples.", 10) % 10
stdout:
<bound method GNUTranslations.gettext of <gettext.GNUTranslations instance at 0x7f8bb7bf1e18>>
<bound method GNUTranslations.ngettext of <gettext.GNUTranslations instance at 0x7f8bb7bf1e18>>
Ahoj světe.
Mám %d jabko.
Mám 1 jabko.
Mám %d jabka.
Mám %d jablek.
Mám 10 jablek.
Doba běhu: 38.0 ms