gettext again

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cantabile

    gettext again

    Hi,
    I'm failing to make it work but can't find out what's wrong. Here's what
    I do :

    =============== =
    test.py

    import gettext
    gettext.install ('')

    msg = _("Message without accented characters")

    print msg
    =============== =

    Then I do :

    xgettext test.py

    mv message.po message pot

    msginit --> output message.fr_FR@e uro

    I edit my fr_FR@euro with this text :
    msgid "This is a message without accented characters."
    msgstr "Message avec caractères accentués : éèàùëêôîaâç"

    Then msgfmt fr_FR@euro

    Then python test.py shows "Message without accented characters"

    How come ? What's wrong with what I am doing ?

    Thanks for your help.
  • stasz

    #2
    Re: gettext again

    On Sun, 07 Aug 2005 11:09:14 +0200, cantabile wrote:
    [color=blue]
    > Hi,
    > I'm failing to make it work but can't find out what's wrong. Here's what
    > I do :[/color]
    [....][color=blue]
    > How come ? What's wrong with what I am doing ?[/color]
    Start with this little howto about gettext.


    And then do this in your test.py:
    The official home of the Python Programming Language


    You should read the part about gettext in the Python Library Reference
    it's really good :-)

    Good luck,
    Stas Z


    Comment

    • cantabile

      #3
      Re: gettext again

      stasz a écrit :[color=blue]
      > On Sun, 07 Aug 2005 11:09:14 +0200, cantabile wrote:
      >
      >[color=green]
      >>Hi,
      >>I'm failing to make it work but can't find out what's wrong. Here's what
      >>I do :[/color]
      >
      > [....]
      >[color=green]
      >>How come ? What's wrong with what I am doing ?[/color]
      >
      > Start with this little howto about gettext.
      > http://childsplay.sourceforge.net/translate-howto.html
      >
      > And then do this in your test.py:
      > http://www.python.org/doc/2.4.1/lib/node330.html
      >
      > You should read the part about gettext in the Python Library Reference
      > it's really good :-)
      >
      > Good luck,
      > Stas Z
      >
      >[/color]

      Well, I must be dumb, because I did exactly that and it still doesn't
      work...

      BTW, I have no pygettext module. I asked here and somebody said it was
      deprecated and now included in xgettext.
      And I've read the Python doc about gettext about ten times, but it seems
      quite outdated since it calls pygettext (as staded above)...

      I've read the info pages of gettext too (quite a long work).

      Here's my test1.py file again :

      =============== ============
      import gettext, os, locale

      locale.setlocal e(locale.LC_ALL )
      gettext.install ('test1.py', '/usr/share/locale')

      msg = _("This is a message without accented characters.")

      print msg
      =============== ============

      What can I do now ?

      Comment

      • stasz

        #4
        Re: gettext again

        On Sun, 07 Aug 2005 21:33:21 +0200, cantabile wrote:
        [color=blue]
        > stasz a écrit :[color=green]
        >> On Sun, 07 Aug 2005 11:09:14 +0200, cantabile wrote:
        >>
        >>[color=darkred]
        >>>Hi,
        >>>I'm failing to make it work but can't find out what's wrong. Here's what
        >>>I do :[/color]
        >>
        >> [....]
        >>[color=darkred]
        >>>How come ? What's wrong with what I am doing ?[/color]
        >>
        >> Start with this little howto about gettext.
        >> http://childsplay.sourceforge.net/translate-howto.html
        >>
        >> And then do this in your test.py:
        >> http://www.python.org/doc/2.4.1/lib/node330.html
        >>
        >> You should read the part about gettext in the Python Library Reference
        >> it's really good :-)
        >>
        >> Good luck,
        >> Stas Z
        >>
        >>[/color]
        >
        > Well, I must be dumb, because I did exactly that and it still doesn't
        > work...
        >
        > BTW, I have no pygettext module. I asked here and somebody said it was
        > deprecated and now included in xgettext.
        > And I've read the Python doc about gettext about ten times, but it seems
        > quite outdated since it calls pygettext (as staded above)...
        >
        > I've read the info pages of gettext too (quite a long work).[/color]
        Ok, you should use xgettext to create a .po file from your test1.py.
        Translate that file and use msgfmt to compile it into a .mo file.
        Copy this .mo file to /usr/share/locale/fr/LC_MESSAGES/
        [color=blue]
        > Here's my test1.py file again :
        >
        > =============== ============
        > import gettext, os, locale
        >
        > locale.setlocal e(locale.LC_ALL )
        > gettext.install ('test1.py', '/usr/share/locale')[/color]
        test1.py is wrong, you must give the name of the .mo file.
        It must be: gettext.install ('test1.mo', '/usr/share/locale')
        Assuming you have called the mo file like that.

        Stas Z


        Comment

        • cantabile

          #5
          Re: gettext again

          stasz a écrit :[color=blue]
          > On Sun, 07 Aug 2005 21:33:21 +0200, cantabile wrote:
          >
          >[color=green]
          >>stasz a écrit :
          >>[color=darkred]
          >>>On Sun, 07 Aug 2005 11:09:14 +0200, cantabile wrote:
          >>>
          >>>
          >>>
          >>>>Hi,
          >>>>I'm failing to make it work but can't find out what's wrong. Here's what
          >>>>I do :
          >>>
          >>>[....]
          >>>
          >>>
          >>>>How come ? What's wrong with what I am doing ?
          >>>
          >>>Start with this little howto about gettext.
          >>>http://childsplay.sourceforge.net/translate-howto.html
          >>>
          >>>And then do this in your test.py:
          >>>http://www.python.org/doc/2.4.1/lib/node330.html
          >>>
          >>>You should read the part about gettext in the Python Library Reference
          >>>it's really good :-)
          >>>
          >>>Good luck,
          >>>Stas Z
          >>>
          >>>[/color]
          >>
          >>Well, I must be dumb, because I did exactly that and it still doesn't
          >>work...
          >>
          >>BTW, I have no pygettext module. I asked here and somebody said it was
          >>deprecated and now included in xgettext.
          >>And I've read the Python doc about gettext about ten times, but it seems
          >>quite outdated since it calls pygettext (as staded above)...
          >>
          >>I've read the info pages of gettext too (quite a long work).[/color]
          >
          > Ok, you should use xgettext to create a .po file from your test1.py.
          > Translate that file and use msgfmt to compile it into a .mo file.
          > Copy this .mo file to /usr/share/locale/fr/LC_MESSAGES/
          >
          >[color=green]
          >>Here's my test1.py file again :
          >>
          >>============= ==============
          >>import gettext, os, locale
          >>
          >>locale.setloc ale(locale.LC_A LL)
          >>gettext.insta ll('test1.py', '/usr/share/locale')[/color]
          >
          > test1.py is wrong, you must give the name of the .mo file.
          > It must be: gettext.install ('test1.mo', '/usr/share/locale')
          > Assuming you have called the mo file like that.
          >
          > Stas Z
          >
          >[/color]

          Well, I did this exactly... and still no go.

          Is this sequence correct :
          1° xgettext test1.py
          2° mv messages.po messages.pot
          3° msginit
          4° Translate msgstr"" in fr_FR@euro.po file
          5° msgfmt -o test1.mo fr_FR@euro.po
          6° cp test1.mo /usr/share/locale/LC_MESSAGES
          7° python test1.py


          This is what I did, without success. Must be bewitched :((

          Comment

          • stasz

            #6
            Re: gettext again

            On Mon, 08 Aug 2005 02:56:35 +0200, cantabile wrote:
            [color=blue]
            > stasz a écrit :[color=green]
            >> On Sun, 07 Aug 2005 21:33:21 +0200, cantabile wrote:
            >>
            >>[color=darkred]
            >>>stasz a écrit :
            >>>
            >>>>On Sun, 07 Aug 2005 11:09:14 +0200, cantabile wrote:
            >>>>
            >>>>
            >>>>
            >>>>>Hi,
            >>>>>I'm failing to make it work but can't find out what's wrong. Here's what
            >>>>>I do :
            >>>>
            >>>>[....]
            >>>>
            >>>>
            >>>>>How come ? What's wrong with what I am doing ?
            >>>>
            >>>>Start with this little howto about gettext.
            >>>>http://childsplay.sourceforge.net/translate-howto.html
            >>>>
            >>>>And then do this in your test.py:
            >>>>http://www.python.org/doc/2.4.1/lib/node330.html
            >>>>
            >>>>You should read the part about gettext in the Python Library Reference
            >>>>it's really good :-)
            >>>>
            >>>>Good luck,
            >>>>Stas Z
            >>>>
            >>>>
            >>>
            >>>Well, I must be dumb, because I did exactly that and it still doesn't
            >>>work...
            >>>
            >>>BTW, I have no pygettext module. I asked here and somebody said it was
            >>>deprecated and now included in xgettext.
            >>>And I've read the Python doc about gettext about ten times, but it seems
            >>>quite outdated since it calls pygettext (as staded above)...
            >>>
            >>>I've read the info pages of gettext too (quite a long work).[/color]
            >>
            >> Ok, you should use xgettext to create a .po file from your test1.py.
            >> Translate that file and use msgfmt to compile it into a .mo file.
            >> Copy this .mo file to /usr/share/locale/fr/LC_MESSAGES/
            >>
            >>[color=darkred]
            >>>Here's my test1.py file again :
            >>>
            >>>============ ===============
            >>>import gettext, os, locale
            >>>
            >>>locale.setlo cale(locale.LC_ ALL)
            >>>gettext.inst all('test1.py', '/usr/share/locale')[/color]
            >>
            >> test1.py is wrong, you must give the name of the .mo file.
            >> It must be: gettext.install ('test1.mo', '/usr/share/locale')
            >> Assuming you have called the mo file like that.
            >>
            >> Stas Z
            >>
            >>[/color]
            >
            > Well, I did this exactly... and still no go.
            >
            > Is this sequence correct :
            > 1° xgettext test1.py
            > 2° mv messages.po messages.pot
            > 3° msginit
            > 4° Translate msgstr"" in fr_FR@euro.po file
            > 5° msgfmt -o test1.mo fr_FR@euro.po
            > 6° cp test1.mo /usr/share/locale/LC_MESSAGES
            > 7° python test1.py
            >
            >
            > This is what I did, without success. Must be bewitched :(([/color]
            Your steps seems alright.
            Just a thought; you do start test1.py from a fr_FR@euro
            environment do you?
            I mean in a xterm do: export LANG=fr_FR@euro
            And then start test1.py from there.

            Stas



            Comment

            • cantabile

              #7
              Re: gettext again

              stasz a écrit :
              [color=blue]
              > Your steps seems alright.
              > Just a thought; you do start test1.py from a fr_FR@euro
              > environment do you?
              > I mean in a xterm do: export LANG=fr_FR@euro
              > And then start test1.py from there.
              >
              > Stas[/color]

              Whoooot ! Working at last, after three days... It wasn't the LANG param
              which is fr_FR@euro allright. I inadvertantly suppressed the '.mo' part
              of gettext.install (test1.mo') and it just worked.
              I guess gettext was looking for a 'test1.mo.mo' file or something.

              Anyways, you made my day my friend !
              Many many thanks !

              Comment

              • cantabile

                #8
                Re: gettext again

                BTW stasz,
                Maybe you'll have still some time for the following question. Trying my
                luck :))

                Suppose I have several units (.py files), say test.py test1.py tets2.py
                , test.py being my main file.
                I've read I can import gettext and install in the main unit. Then, must
                I create .po files for each unit or is there a way to make a single .po
                file for the whole app. ?

                Comment

                • stas

                  #9
                  Re: gettext again

                  On Mon, 08 Aug 2005 17:01:21 +0200, cantabile wrote:
                  [color=blue]
                  > stasz a écrit :[/color]
                  [color=blue]
                  > Whoooot ! Working at last, after three days... It wasn't the LANG param[/color]
                  [...][color=blue]
                  > Anyways, you made my day my friend !
                  > Many many thanks ![/color]
                  Your welcome :-)

                  Stas

                  Comment

                  • stas

                    #10
                    Re: gettext again

                    On Mon, 08 Aug 2005 17:39:34 +0200, cantabile wrote:
                    [color=blue]
                    > BTW stasz,
                    > Maybe you'll have still some time for the following question. Trying my
                    > luck :))
                    >
                    > Suppose I have several units (.py files), say test.py test1.py tets2.py
                    > , test.py being my main file.
                    > I've read I can import gettext and install in the main unit. Then, must
                    > I create .po files for each unit or is there a way to make a single .po
                    > file for the whole app. ?[/color]
                    Yes there is.

                    As a reminder, make sure that you install gettext in the namespace
                    of your toplevel module.
                    What I mean is this:

                    test1.py imports test2.py and test3.py
                    test2.py imports test4.py

                    Now you have to place the gettext.install call in test1.py and
                    then the other modules can access it.
                    If you place the gettext call in, for example, test2.py then only
                    test2.py and test4.py have access to it.
                    So when it don't work as you expect make sure to check if every
                    module has access to the "_" from gettext.

                    So now for your question about one po/mo file for multiple
                    modules.
                    I use this file for a project of mine it also includes some
                    explanation in case I forget it myself :-)
                    <shameless plug>
                    The project is called "Guido van Robot".

                    </shameless plug>

                    The files mentioned inside this file are used in the above project
                    and serve as an example in this case.
                    You could replace the files with your own to try it.
                    The file itself is called "FilesForTr ans" so you could put this
                    file together with your test modules inside a directory and follow
                    the instructions in side "FilesForTrans" .

                    Stas Z


                    Contents of "FilesForTr ans"
                    ---- cut here ---------
                    ## These files contain i18n strings
                    ## Use: xgettext -f ./FilesForTrans -p /tmp
                    ## The message.po will be located in /tmp
                    ## Now do: msgmerge ./po/nl/gvr.po /tmp/messages.po > /tmp/new_nl.po
                    ## compile the .po to mo (place the .po after editing in ./po/nl/)
                    ## msgfmt ./po/nl/gvr.po -o ./locale/nl/LC_MESSAGES/gvr.mo

                    gvr.py
                    gvrparser.py
                    world.py
                    worldMap.py
                    Editorwindows.p y





                    Comment

                    • cantabile

                      #11
                      Re: gettext again

                      Ok, I'll try that.
                      Thanks again Stasz !

                      Comment

                      • cantabile

                        #12
                        Re: gettext again

                        stas a écrit :
                        [color=blue]
                        > As a reminder, make sure that you install gettext in the namespace
                        > of your toplevel module.
                        > What I mean is this:
                        >
                        > test1.py imports test2.py and test3.py
                        > test2.py imports test4.py
                        >
                        > Now you have to place the gettext.install call in test1.py and
                        > then the other modules can access it.
                        > If you place the gettext call in, for example, test2.py then only
                        > test2.py and test4.py have access to it.
                        > So when it don't work as you expect make sure to check if every
                        > module has access to the "_" from gettext.[/color]
                        Noticed something :
                        I must import test2.py AFTER gettext.install ('test1') and even then, if
                        test3.py imports test2.py, messages won't be translated in test3.py. I
                        have to import test3.py in test1.py too.
                        Is this normal behaviour or is there something I'm missing (again) ?

                        PS : your project looks nice !

                        Comment

                        • stasz

                          #13
                          Re: gettext again

                          On Tue, 09 Aug 2005 23:59:26 +0200, cantabile wrote:
                          [color=blue]
                          > stas a écrit :
                          >[color=green]
                          >> As a reminder, make sure that you install gettext in the namespace
                          >> of your toplevel module.[/color][/color]
                          [....][color=blue]
                          > Noticed something :
                          > I must import test2.py AFTER gettext.install ('test1') and even then, if
                          > test3.py imports test2.py, messages won't be translated in test3.py. I
                          > have to import test3.py in test1.py too.
                          > Is this normal behaviour or is there something I'm missing (again) ?[/color]
                          Hmm, perhaps I missed something, namespaces can be sometimes hard
                          to get right.
                          Let's see:

                          test1 imports and installs gettext.
                          Now test1 holds the gettext reference.
                          test1 imports test2 and test3 and 'inherits' test1 namespace
                          test2 and test3 have access to test1 namespace and the gettext.

                          So in (pseudo) Python code test1 would looks like this:

                          import gettext
                          gettext.install (test)# assuming test.mo holds all the strings
                          import test2, test3

                          Another approach would be to place the whole gettext install stuff
                          in a separate module wrapped in a function which can be imported
                          by every module that wants language support.
                          (That's the approach I always use)

                          Contents of utils.py
                          (Code is not tested, it's to show the principals)

                          import locale, gettext
                          def set_gettext(mo_ location='/usr/share/locale'):
                          locale.setlocal e(locale.LC_ALL )
                          gettext.install ('test1', mo_location)

                          Now in your modules you could do this:

                          In test1.py:
                          from utils import set_gettext
                          set_gettext()# remember gettext.install installs '_' in the
                          # current namespace, in this case test1.
                          import test2, test 3

                          In test4.py
                          from utils import set_gettext
                          set_gettext()
                          .....
                          .....

                          Now test1 and test2 get their gettext from test1 while test4 import
                          gettext from utils.py which installs the same mo file as it did in
                          test1, test2 and test3.
                          [color=blue]
                          > PS : your project looks nice ![/color]
                          Thanks.

                          BTW, the principal of gettext in utils.py is also implemented in
                          the gvr project, so you have a real life example there :-)

                          Stas

                          Comment

                          • cantabile

                            #14
                            Re: gettext again

                            stasz a écrit :[color=blue]
                            > On Tue, 09 Aug 2005 23:59:26 +0200, cantabile wrote:
                            >
                            >[color=green]
                            >>stas a écrit :
                            >>
                            >>[color=darkred]
                            >>>As a reminder, make sure that you install gettext in the namespace
                            >>>of your toplevel module.[/color][/color]
                            >
                            > [....]
                            >[color=green]
                            >>Noticed something :
                            >>I must import test2.py AFTER gettext.install ('test1') and even then, if
                            >>test3.py imports test2.py, messages won't be translated in test3.py. I
                            >>have to import test3.py in test1.py too.
                            >>Is this normal behaviour or is there something I'm missing (again) ?[/color]
                            >
                            > Hmm, perhaps I missed something, namespaces can be sometimes hard
                            > to get right.
                            > Let's see:
                            >
                            > test1 imports and installs gettext.
                            > Now test1 holds the gettext reference.
                            > test1 imports test2 and test3 and 'inherits' test1 namespace
                            > test2 and test3 have access to test1 namespace and the gettext.
                            >
                            > So in (pseudo) Python code test1 would looks like this:
                            >
                            > import gettext
                            > gettext.install (test)# assuming test.mo holds all the strings
                            > import test2, test3
                            >
                            > Another approach would be to place the whole gettext install stuff
                            > in a separate module wrapped in a function which can be imported
                            > by every module that wants language support.
                            > (That's the approach I always use)
                            >
                            > Contents of utils.py
                            > (Code is not tested, it's to show the principals)
                            >
                            > import locale, gettext
                            > def set_gettext(mo_ location='/usr/share/locale'):
                            > locale.setlocal e(locale.LC_ALL )
                            > gettext.install ('test1', mo_location)
                            >
                            > Now in your modules you could do this:
                            >
                            > In test1.py:
                            > from utils import set_gettext
                            > set_gettext()# remember gettext.install installs '_' in the
                            > # current namespace, in this case test1.
                            > import test2, test 3
                            >
                            > In test4.py
                            > from utils import set_gettext
                            > set_gettext()
                            > ....
                            > ....
                            >
                            > Now test1 and test2 get their gettext from test1 while test4 import
                            > gettext from utils.py which installs the same mo file as it did in
                            > test1, test2 and test3.
                            >
                            >[color=green]
                            >>PS : your project looks nice ![/color]
                            >
                            > Thanks.
                            >
                            > BTW, the principal of gettext in utils.py is also implemented in
                            > the gvr project, so you have a real life example there :-)
                            >
                            > Stas
                            >[/color]
                            Thanks for the explanation, very clear, as usual.
                            Cheers :)

                            Comment

                            Working...