Module: Translating

Extended by:
Logging
Included in:
ArgParser, Configuration, SQLite2DBF
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

@@log =

intitialize class variabes (static attributes)

init_logger(STDOUT)
@@awild =

There are better ways to extend a translated string, but I keep the ‘wild-card’ for historical reasons. TODO: get rid of this crap!

'XX'
@@lang =
nil
@@lang_file =

TODO: reflect about this. The possibility to write a LANG-file is okay, sometimes. Most of the time it is not.

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

init_logger, log_level=, log_target=

Methods included from File_Checking

#file_check, file_check

Class Method Details

.languageObject

find the current language-setting and return it.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/translating.rb', line 59

def self.language()
  if @@lang == nil
    r = ENV['LANG']
    if(r)
      @@lang = r[0, 2]
    elsif( !File_Checking::file_check(@@lang_file, [:exist?, :readable?]) && 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, *args) ⇒ Object

Translate a string to the currently set langage. The args parameter may contain replacement-text which will appear at the positions indicated by wildcard-characters in the original string.

TODO: Come on! sprintf-syntax is far better than the awkward ‘wildcard’. Just remove this crap.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/translating.rb', line 79

def self.trl(t, *args)
  
@@log.debug('@tr is ' << @@tr.to_s) 
  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
  if(args && !args.empty?)
    i = -1 
    lt = lt.gsub(@@awild) do |a| 
      i += 1
      args.flatten[i]
    end
    lt += args[i + 1, args.length].join
  end
  return lt
end

Instance Method Details

#trl(t, *args) ⇒ Object

Translate a string to the currently set langage. The args parameter may contain replacement-text which will appear at the positions indicated by wildcard-characters in the original string.



106
107
108
# File 'lib/translating.rb', line 106

def trl(t, *args )
  Translating::trl(t, args)
end