[Back] [English] [Japanese]

Ruby GetText Package

What is Ruby GetText Package?

Ruby GetText Package is Native Language Support Library and Tools which modeled after GNU gettext package.

Requirements

 (Development times only)

Download

ruby-gettext-package-0.2.1.tar.gz

Install

De-Compress archive and enter its top directory.
Then type:

# ruby install.rb config
# ruby install.rb setup
# ruby install.rb install

You can also install files in your favor directory by
supplying install.rb some options. Try "ruby install.rb --help".

Basic term explanation

pot-file, po-file

They are the text files with msgid(Original message) and msgstr(Translated message). You can create it with rgettext from your script file.

mo-file

This is binary files which created from po-files by msgfmt.

domain(TextDomain)

domain is just mo-file name. Usually, it is called PACKAGE(like in GNOME).

API Reference

module GetText

GetText.bindtextdomain(domainname, path = nil, locale = nil)
Bind TextDomain to #{path}/#{locale}/LC_MESSAGES/#{domainname}.mo
GetText._(msgid)
GetText.gettext(msgid)
Return localized text by msgid. If there are not binded mo-file, it will return msgid.
GetText.N_(msgid)
Return msgid. This method does nothing. But it is required in order to recognize the msgid by rgettext.
GetText.setlocale(locale)
Set locale(String) like "C", "de", "fr", "it", "ko", "ja_JP.eucJP", "zh_CN.EUC" ...

rgettext

rgettext is the tool which create po-files from your Ruby Scripts.

Usage

$rgettext hoge.rb -o hoge.po

Development procedure

Create hello.rb

For example, write hello.rb as follows.

require 'gettext'

include GetText

bindtextdomain("hello")
print _("Hello World\n")

Pick up strings in the script(create a po-file)

Create a po-file with rgettext.

$rgettext hello.rb -o hello.pot

Translate hello.pot into <your language>.po.

This example for Japanese(not 2-byte string but 1-byte alphabet).

$cp hello.pot ja.po

Then edit ja.po as follows.

#: ../hello.rb:7
msgid "Hello World\n"
msgstr "KON-NICHI-WA SEKAI\n"

In addition, you can use the some tools in GNU gettext package for maintainig po-file.

* How to use GetText.N_(msgid)

GetText.N_(msgid) is used with GetText._(msgid).

require 'gettext'

include GetText
bindtextdomain("hello_noop")

msgs = [N_("Hello World"), N_("Hello World2")]

msgs.each do |msg|
  print _(msg), "\n"
end

In this case, rgettext can not recognize _(msg) as msgid.
But since you use GetText.N_(msgid) to define the messages, it is recognized as msgid by rgettext.

* If are there "fuzzy" comments?

If po-file have some fuzzy comments as follows:

#: hello.rb:7
#, fuzzy
msgid "Hello World"
msgstr "KON-NICHI-WA SEKAI"

Then confirm the messages(msgid and msgstr) are correct, and remove "fuzzy" line.

#: hello.rb:7
msgid "Hello World"
msgstr "KON-NICHI-WA SEKAI"

Make a mo-file.

Finally, you make a mo-file, and put it into a path properly.
In this example, you don't set path in bindtextdomain(), GetText search mo-file in /usr/share/locale/#{lang}/LC_MESSAGES/ or /usr/local/share/locale/#{lang}/LC_MESSAGES/. So you put it into one of them.
Here, you put it into /usr/local/share/locale/ja/LC_MESSAGES/.

$msgfmt ja.po -o /usr/local/share/locale/ja/LC_MESSAGES/

Run the script

$ruby hello.rb

Is it OK? Congratulations!

If you can't show the messages in your language, execute it with -d option.

$ruby -d hello.rb
Search path:["/usr/share/locale", "/usr/local/share/locale"]
locale:"ja_JP.eucJP"

MO file is not found in
      /usr/share/locale/ja_JP.eucJP/LC_MESSAGES/hello.mo
      /usr/share/locale/ja_JP/LC_MESSAGES/hello.mo
      /usr/share/locale/ja/LC_MESSAGES/hello.mo
      /usr/local/share/locale/ja_JP.eucJP/LC_MESSAGES/hello.mo
      /usr/local/share/locale/ja_JP/LC_MESSAGES/hello.mo
      /usr/local/share/locale/ja/LC_MESSAGES/hello.mo

Check hello.mo is in one of those pathes.

Difference from GNU gettext

License

This program is licenced under the same licence as Ruby.
(See the file 'COPYING'.)

mo.rb

Copyright (C) 2001 Masahiro Sakai <s01397ms@sfc.keio.ac.jp>

gettext.rb

Copyright (C) 2001,2002 Masahiro Sakai <s01397ms@sfc.keio.ac.jp>, Masao Mutoh <mutoh@highwhay.ne.jp>

rgettext

Copyright (C) 2001,2002 Yasushi Shoji <yashi@yashi.com>, Masao Mutoh <mutoh@highwhay.ne.jp>

install.rb

Copyright (C) 2000,2001 Minero Aoki <aamine@loveruby.net>
This file is released under LGPL. See the top of the install.rb.

Ruby GetText Package

Copyright (C) 2001,2002 Masao Mutoh <mutoh@highwhay.ne.jp>

Maintainer

Masao Mutoh <mutoh@highway.ne.jp>

ChangeLog

2002-02-22 Masao Mutoh <mutoh@highway.ne.jp>

2002-02-21 Masao Mutoh <mutoh@highway.ne.jp>

2002-02-13 Masao Mutoh <mutoh@highway.ne.jp>

2002-02-03 Masao Mutoh <mutoh@highway.ne.jp>

2002-01-06 Masao Mutoh <mutoh@highway.ne.jp>

2002-01-01 Masao Mutoh <mutoh@highway.ne.jp>

2001-12-24 Masao Mutoh <mutoh@highway.ne.jp>


$Id: ruby-gettext.rd,v 1.13 2002/02/22 14:15:35 mutoh Exp $