Contents

Gettext oddities with Ruby

Contents

I was having a lot of trouble with gettext in Ruby, mostly due to lacking documentation. Here are some useful things I figured out while writing TTime. I ended up having a single gettext_settings.rb, included from every file which uses gettext. Here it is (with some extra notes)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/ruby
begin
  require 'gettext'
  require 'pathname'

  include GetText

  # This fixes a swarm of problems on Windows
  GetText.locale.charset = "UTF-8"

  # Ruby's gettext acts in a sane
  # method - add a path to the set of paths
  # scanned.
  locale_in_data_path = Pathname.new($0).dirname + \
    "../data/locale/%{locale}/LC_MESSAGES/%{name}.mo"
  add_default_locale_path(locale_in_data_path.to_s)
  bound_text_domain = bindtextdomain("ttime")

  # For Glade, however, it only seems to
  # be possible to specify one path at a
  # time. Fortunately, gettext already
  # found it for us.
  my_current_mo = bound_text_domain.entries[0].current_mo
  if my_current_mo
    ENV["GETTEXT_PATH"] = my_current_mo.filename.gsub(
      /locale\/[^\/]+\/LC_MESSAGES.*/,
      "locale/")
  end
rescue LoadError
  def _ s #:nodoc:
    # No gettext? No problem.
    s
  end
end

One note for context: I use setup.rb (and ruby-pkg-tools) to package TTime. So my localizations go in data/locale.