Class: Knj::Gettext_threadded

Inherits:
Object
  • Object
show all
Defined in:
lib/knj/gettext_threadded.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Gettext_threadded

Returns a new instance of Gettext_threadded.



4
5
6
7
8
9
10
11
# File 'lib/knj/gettext_threadded.rb', line 4

def initialize(args = {})
  @args = {
    :encoding => "utf-8"
  }.merge(args)
  @langs = {}
  @dirs = []
  load_dir(@args["dir"]) if @args["dir"]
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



2
3
4
# File 'lib/knj/gettext_threadded.rb', line 2

def args
  @args
end

#langsObject (readonly)

Returns the value of attribute langs.



2
3
4
# File 'lib/knj/gettext_threadded.rb', line 2

def langs
  @langs
end

Instance Method Details

#gettext(str, locale) ⇒ Object

This function can be used to make your string be recognized by gettext tools.



60
61
62
# File 'lib/knj/gettext_threadded.rb', line 60

def gettext(str, locale)
  return trans(locale, str)
end

#lang_optsObject

Returns a hash with the language ID string as key and the language human-readable-title as value.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/knj/gettext_threadded.rb', line 65

def lang_opts
  langs = {}
  @langs.keys.sort.each do |lang|
    title = nil
    
    @dirs.each do |dir|
      title_file_path = "#{dir}/#{lang}/title.txt"
      if File.exists?(title_file_path)
        title = File.read(title_file_path, {:encoding => @args[:encoding]}).to_s.strip
      else
        title = lang.to_s.strip
      end
      
      break if title
    end
    
    langs[lang] = title
  end
  
  return langs
end

#load_dir(dir) ⇒ Object

Loads a ‘locales’-directory with .mo- and .po-files.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/knj/gettext_threadded.rb', line 14

def load_dir(dir)
  @dirs << dir
  check_folders = ["LC_MESSAGES", "LC_ALL"]
  
  Dir.new(dir).each do |file|
    fn = "#{dir}/#{file}"
    if File.directory?(fn) and file.match(/^[a-z]{2}_[A-Z]{2}$/)
      @langs[file] = {} if !@langs[file]
      
      check_folders.each do |fname|
        fpath = "#{dir}/#{file}/#{fname}"
        
        if File.exists?(fpath) and File.directory?(fpath)
          Dir.new(fpath).each do |pofile|
            if pofile.match(/\.po$/)
              pofn = "#{dir}/#{file}/#{fname}/#{pofile}"
              
              cont = nil
              File.open(pofn, {:encoding => @args[:encoding]}) do |fp|
                cont = fp.read
              end
              
              cont.scan(/msgid\s+\"(.+)\"\nmsgstr\s+\"(.+)\"\n\n/) do |match|
                @langs[file][match[0]] = match[1]
              end
            end
          end
        end
      end
    end
  end
end

#trans(locale, str) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/knj/gettext_threadded.rb', line 47

def trans(locale, str)
  locale = locale.to_s
  str = str.to_s
  
  if !@langs.key?(locale)
    raise "Locale was not found: '#{locale}' in '#{@langs.keys.join(", ")}'."
  end
  
  return str if !@langs[locale].key?(str)
  return @langs[locale][str]
end