Module: Translating

Included in:
Adder, ArgParser, SprichWoerter
Defined in:
lib/translating.rb

Overview

A way to produce translated text in a ruby-program. Translations are read from a file “translations” in the program folder.

Constant Summary collapse

@@lang =

There are better ways to extend a translated string, but I keep the ‘wild-card’ for historical reasons.

nil
@@lang_file =
format("%s%s", RD, 'LANG')
@@tr =
YAML::load_file("#{RD}translations")

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.languageObject

find the current language-setting and return it.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/translating.rb', line 40

def self.language()
  if @@lang == nil
    r = ENV['LANG']
    if(r)
      @@lang = r[0, 2]
    elsif( File.exist?(@@lang_file) && File.readable?(@@lang_file) && File::size(@@lang_file) >= 2)
      File::open(@@lang_file, 'r') {|f| @@lang = f.readline}
      @@lang.chomp!.downcase! if @@lang
    end
  end
  @@lang = 'en' if !@@lang
end

.trl(t) ⇒ Object

Translate a string to the currently set langage. Note that the argument may contain a format-string in sprintf-syntax.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/translating.rb', line 56

def self.trl(t)
  Translating::language()
  lt = @@tr[t]
  if(lt)
    lt = lt[@@lang]
  else
    # File.open('/tmp/mtf', 'a+') {|f| f << t << "\n"}
    puts "\nTRANSLATION MISSING: \"" << t << "\""
  end
  lt ||= t
  return lt
end

.trl_words(str) ⇒ Object

Translate in the given string only those words for which a translation is found. This is useful for mixed content like dates: “Friday, 8. July 2011”. As the order changes with the locale, alphabetic content is matched and the remainder discarded.



81
82
83
84
85
86
87
88
89
90
# File 'lib/translating.rb', line 81

def self::trl_words(str)
  n_str = str.clone
  loop do 
    m = str.match(/[[:alpha:]]+/)
    break if !m
    str.gsub!(m.to_s, '')
    n_str.gsub!(m.to_s, trl(m.to_s))
  end
  return n_str
end

Instance Method Details

#trl(t) ⇒ Object

Translate a string to the currently set langage. Note that the argument may contain a format-string in sprintf-syntax.



72
73
74
# File 'lib/translating.rb', line 72

def trl(t )
  Translating::trl(t)
end

#trl_words(str) ⇒ Object

Translate in the given string only those words for which a translation exists. This is useful for mixed content like dates: “Friday, 8. July 2011”. As the order changes with the locale, alphabetic content is matched and the remainder discarded.



96
97
98
# File 'lib/translating.rb', line 96

def trl_words(str)
  Translating::trl_words(str)
end