module Translating

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

Public Class Methods

language() click to toggle source

find the current language-setting and return it.

# File translating.rb, line 42
def self.language()
  if( !File.exist?(@@lang_file) || !File::readable?(@@lang_file) || File::size(@@lang_file) < 2)
    # Only if there is no LANG-file
    if @@lang == nil
      # ... try the environment setting
      r = ENV['LANG']
      if(r)
        @@lang = r[0, 2]
      end
    end
  else
    # ... else read the code from the file
    File::open(@@lang_file, 'r') {|f| @@lang = f.readline}
    @@lang.chomp!.downcase! if @@lang
  end
  # ... and if nothing works, set language to English.
  @@lang ||= 'en' if !@@lang
end
trl(t) click to toggle source

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

# File translating.rb, line 64
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) click to toggle source

2019 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.

# File translating.rb, line 90
def self::trl_words(str)
  n_str = str.clone
  loop do 
    m = str.match(/[[:alpha:]]+/)
    break if !m
    ### allow the loop to break
    str.gsub!(m.to_s, '')
    n_str.gsub!(m.to_s, trl(m.to_s))
  end
  return n_str
end

Public Instance Methods

trl(t ) click to toggle source

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

# File translating.rb, line 80
def trl(t )
  Translating::trl(t)
end
trl_words(str) click to toggle source

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.

# File translating.rb, line 106
def trl_words(str)
  Translating::trl_words(str)
end