Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Login


How to write the non-ascii texts

All strings you pass to GTK+ functions must be in UTF-8. Of course, Ruby-GNOME2 has the same requirement.

Usually, you have written ruby scripts in your locale and charset, which isn't necessarily UTF-8.

So you may be confused about how to write texts in Ruby-GNOME2 at first.

Here is a summary of your options:

1 Write scripts in UTF-8

This is the easiest way if you use an editor that supports UTF-8.

You need to define $KCODE = 'u' in the head of your script.

$KCODE = 'u'
require 'gtk2'


Also you can use the -K option to Ruby.

2 Write the script in your charset and convert it with iconv

Write your script with your favorite editor which doesn't support UTF-8. You save it with your own (non-UTF-8) charset.

Then you convert it with iconv when you run it.

iconv -f <your charset> -t utf-8 foo.rb |ruby -
iconv -f euc-jp -t utf-8 foo.rb |ruby -

Effectively, this is the same as the first method. So you need to define $KCODE = 'u' in your script.

3 Use GLib.convert for each string.

GLib.convert converts a string from one character set to another.

require 'gtk2'"RYOUKAI", "utf-8", "euc-jp"))  #str, to, from

I don't recommand this way for static texts. Usually you should use GLib.convert when you read non-ascii, non-utf-8 texts from files or networks.

4 Ruby-GetText-Package

So far, they don't support i18n (L10n). It may be no problem if you distribute your scripts locally. But if you want to distribute it whole the world, you need to support i18n (L10n).

Ruby-GetText-Package is the one of the solutions which automates replacing English text with localized text. It's also compatible with the GNU gettext Package. Try it if you are interested.


2003-08-29 Masao Mutoh
Change the treatments of $KCODE="u"
2003-07-08 Evan Martin
Fixed up the English
2003-07-08 Masao Mutoh
Initial Release
Last modified:2011/06/11 10:23:56