[ruby-gnome2-doc-cvs] [Hiki] create - tips_libglade_i18n

Back to archive index

ruby-****@sourc***** ruby-****@sourc*****
2004年 4月 21日 (水) 23:26:58 JST


-------------------------
REMOTE_ADDR = 217.82.199.232
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/de/?tips_libglade_i18n
-------------------------
=Internationalisation (i18n) in Ruby/libglade unterstzen

==Einfrung
Dieses Tutorial zeigt, wie man auch mit Ruby/libglade die Vorteile von per Gettext ersetzten Strings nutzen kann. F diejenigen, die mit dem Konzept von Gettext nicht vertraut sind, empfiehlt sich die Lekte des ((<Handbuches|URL:http://www.gnu.org/software/gettext/manual>)).

Bevor es losgeht, stellen Sie bitte sicher, das Ruby-GNOME2 auf ihrem Rechner installiert ist.

Der ursprgliche Autor dieses Hilfetextes benutzte Ruby 1.8.1, Ruby-GNOME2 0.8.1 und gettext 0.12.1 auf einem Gentoo-Linux-Rechner. Falls etwas bei Ihnen nicht funktioniert wie hier beschrieben, dann denken Sie daran, dass dies durch Versionsunterschiede verursacht sein knte. Keines der Beispiele wurde unter MS Windows getestet; es w舐e natlich hilfreich, wenn jemand dem Autor diesbezliche Informationen zugehen lassen knte.

Viele der folgenden Informationen stammen aus der ((<PyGTK FAQ|URL:http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq22.001.htp>)).

==Das Interface in Glade erstellen
Der erste Schritt ist die Erstellung der Benutzeroberfl臘he mithilfe von glade-2. Beachten Sie, dass jedes Widget mit einem Textfeld in der XML-Datei das translatable-Attribut mit dem Wert "yes" besitzt.

W臧len Sie nun unter Projekt -> Einstellungen -> LibGlade-Einstellungen "樅ersetzbare Zeichenketten speichern".
Als Dateinamen geben Sie z.B. glade-msg.c an. Der Output wird ein C-粫nliches Format besitzen, das von xgettext verwendet wird.

== Creating the translations

((*Note*)) this section is heavily borrowed from the ((<PyGTK FAQ|URL:http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq22.001.htp>)).
mentioned above. I included this section so the reader doesn't have to jump around.

Next you'll want to create the main translation file. To do this, use the following
command:

  xgettext -kN_ -o myapp.pot glade-msg.c

Look inside of this file. You should see the glade strings listed. 

This .pot file should be sent to the translators. Once they get the file, they should
create a .po file. As an example, to create a German translation they would use:

  LANG=de_DE msginit

This will create de.po, where the translated strings should be entered. The translation
file needs to be converted to a binary format before use:

  msgfmt de.po -o myapp.mo

This file should then be placed in the appropriate directory. On my machine,
this would go in /usr/share/locale/de/LC_MESSAGES/

== Writing your code

Now to tell Ruby/Libglade to load the locale files. Normally you would have a line
in your code similar to this:

  @glade = GladeXML.new('myapp.glade') { |handler| method(handler) }

To get the translated strings to load, you need to specify a locale name to glade so
it knows which file to load the strings from. Change the above code to:

  @glade = GladeXML.new('myapp.glade', nil, 'myapp') { |handler| method(handler) }

The third parameter, 'myapp', will tell glade to look for 'myapp.mo'. Now to load the
application:

  LANG=de_DE ruby myapp.rb

The translated strings should appear. That's all there is to it.

== Auto-generating the binary translation file

((*Note*)) this section is based on code included with Masao Mutoh's ((<ruby-gettext
distribution|URL:http://ponx.s5.xrea.com/hiki/ruby-gettext.html>)).

For every translation file, you'll need to create a new .mo file. With Minero
Aoki's ((<install.rb|URL:http://i.loveruby.net/en/setup.html>)) this becomes very easy
to automate. 

For this example we will pretend we have German and Spanish translations already
created, named de.po and es.po, respectively.

  cd $PROJECTROOT
  mkdir po
  mkdir po/de
  mkdir po/es
  cp de.po po/de/myapp.po
  cp es.po po/es/myapp.po

Then, in your main directory, create a file named post-setup.rb which contains:

  require 'fileutils'

  podir = srcdir_root + "/po/"
  modir = srcdir_root + "/data/locale/%s/LC_MESSAGES/"

  Dir.glob("po/*/*.po") do |file|
      lang, basename = /po\/([\w\.]*)\/(.*)\.po/.match(file).to_a[1,2]
      FileUtils.mkdir_p modir % lang
      system("msgfmt #{podir}#{lang}/#{basename}.po -o #{modir}#{basename}.mo" % lang)
  end

You may also want to create the following pre-clean.rb:
  
  Dir.glob("data/**/*.mo").each do |file|
      File.delete(file)
  end

Now, when 'ruby install.rb setup' is run, the translation files will be generated.
On 'ruby install.rb install' they will be installed to the correct place.

== Conclusion

I hope this all was decently clear. If people run into problems, feel free to
contact me and I will try to help out. I would like to improve this document,
so also contact me with corrections, etc.

== Author

Zachary P. Landau (kapheine AT hypa DOT net).

The reader should be warned that I am far from an expert on the subject. I only
very recently started trying to add internationalization support to one of my
projects. While searching for information, I found only bits and pieces that
had to be put together. I wrote this tutorial in attempt to keep other people
from having to do the same.

That being said, I'd be very happy to incorporate additions and corrections from
people who know more about the subject. Please contact me with any information
you have.

== ChangeLog

:2004-03-20 Zachary P. Landau
 Initial release





ruby-gnome2-cvs メーリングリストの案内
Back to archive index