ruby-****@sourc*****
ruby-****@sourc*****
2004年 3月 21日 (日) 02:25:01 JST
------------------------- REMOTE_ADDR = 217.117.55.140 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/?tips_libglade_i18n ------------------------- = Supporting internationalization (i18n) in Ruby/Libglade == Introduction This tutorial will show how to have Ruby/Libglade take advantage of gettext translated strings. For those of you unfamiliar with the concept of gettext, reading the ((<manual|URL:http://www.gnu.org/software/gettext/manual>)) is probably a good idea. Before starting, make sure you have Ruby-GNOME2 installed. While writing this tutorial, I used Ruby 1.8.1, Ruby-GNOME2 0.8.1 and gettext 0.12.1 on a Gentoo Linux machine. There may be differences between versions that cause problems, so remember that if something doesn't work for you. I don't know if or how any of this works in Microsoft Windows. If anyone would like to contribute information regarding that, it'd be helpful. A lot of this information was obtained from the ((<PyGTK FAQ|URL:http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq22.001.htp>)). == Creating the interface in Glade The first step is to create the interface with 'glade-2'. Every time you create a widget with a text field, the XML generated will have a 'translatable="yes"' field added to it. Then go to Options->LibGlade Options and select 'Save Translatable Strings'. In the text field, insert a filename to save the strings in 'glade-msg.c' is a good one. The output is in a C-like format, because that is what xgettext will use. == 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 + ((*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 + 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