Module: I18n

Defined in:
lib/wiki_lyrics/i18n/i18n.rb

Constant Summary collapse

@@DEFAULT_LANG =
"en"
@@DEFAULT_ENCODING =
"UTF-8"
@@loaded =
false
@@texts =
{}
@@files_prefix =
File.expand_path( File.dirname( __FILE__ ) )

Class Method Summary collapse

Class Method Details

.get(key, *args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wiki_lyrics/i18n/i18n.rb', line 31

def I18n.get( key, *args )
	I18n.reload() if ! @@loaded
	ret = @@texts[key]
	return key if ret == nil
	if args
		arg_idx = 1
		args.each() do |arg|
			ret = ret.gsub( "%#{arg_idx}", arg.to_s() )
			arg_idx += 1
		end
	end
	return ret
end

.get_local_encodingObject



85
86
87
88
89
90
# File 'lib/wiki_lyrics/i18n/i18n.rb', line 85

def I18n.get_local_encoding()
	encoding = ENV["LANG"].to_s().strip()
	return @@DEFAULT_ENCODING if encoding.empty?
	return @@DEFAULT_ENCODING if ! encoding.sub!( /^.+\./, "" )
	return encoding
end

.get_local_languageObject



75
76
77
78
79
80
81
82
83
# File 'lib/wiki_lyrics/i18n/i18n.rb', line 75

def I18n.get_local_language()
	language = I18n.get_messages_locale()
	language = ENV["LANGUAGE"].to_s().strip() if Strings.empty?( language )
	language = ENV["LANG"].to_s().strip() if Strings.empty?( language )
	return @@DEFAULT_LANG if language.empty?
	language.sub!( /_[^.]+/, "" )
	return @@DEFAULT_LANG if ! language.sub!( /\..+$/, "" )
	return language
end

.get_messages_localeObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/wiki_lyrics/i18n/i18n.rb', line 54

def I18n.get_messages_locale()
	locale = `locale`

	# LC_ALL overrides the values of all the other internationalization variables if set to a non-empty string value
	if (md = /LC_ALL="?([^\n"]*)"?/.match( locale ))
		return md[1] if ! Strings.empty?( md[1] )
	end

	# if set, use LC_MESSAGES
	if (md = /LC_MESSAGES="?([^\n"]*)"?/.match( locale ))
		return md[1] if ! Strings.empty?( md[1] )
	end

	# LANG provides a default value for the internationalization variables that are unset or null
	if (md = /LANG="?([^\n"]*)"?/.match( locale ))
		return md[1] if ! Strings.empty?( md[1] )
	end

	return nil
end

.load_translation(language) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/wiki_lyrics/i18n/i18n.rb', line 98

def I18n.load_translation( language )
	begin
		file = File.new( "#{@@files_prefix}/#{language}.rb", "r" )
		while ( line = file.gets )
			line = @@locale2utf8.iconv( line ) if @@locale2utf8
			if (md = /^ *([a-z\.]+) *= *(.*)$/.match( line ))
				key, value = md[1], md[2].strip()
				next if value.empty?
				@@texts[key] = value
				@@texts[key].gsub!( /\\./ ) do |m|
					char = m.slice( 1, 1 )
					if char == "\\"
						"\\"
					elsif char == "n"
						"\n"
					else
						char
					end
				end
			end
		end
		file.close()
	rescue Errno::ENOENT
		puts "translations file not found: #{language}.rb"
	end
end

.reloadObject



45
46
47
48
49
50
51
# File 'lib/wiki_lyrics/i18n/i18n.rb', line 45

def I18n.reload()
	@@texts = {}
	I18n.load_translation( "en" ) # the default and reference meaning of keys
	language = I18n.get_local_language()
	I18n.load_translation( language ) if language != "en"
	@@loaded = true
end