Class: TT::Babelfish
- Inherits:
-
Object
- Object
- TT::Babelfish
- Defined in:
- TT_Lib2/babelfish.rb
Overview
Translation dictionary. Pass strings through the translate
or
tr
method to translate strings to the language given in to the
last load
call.
Silently eats errors and pass through the original string if a translation cannot be made.
module MyPlugin
# Assigning the instance to a constant makes it into
# a easy to use shorthand that works in any sub-modules/classes.
S = TT::Babelfish.new( l10n_path )
def self.init
S.load( 'fr' )
end
def self.foo
puts S.tr( 'Hello World' )
end
end # module
Instance Attribute Summary collapse
- #default_l10n ⇒ Object readonly
- #file_ex ⇒ Object readonly
- #l10n ⇒ Object readonly
- #path ⇒ Object readonly
Instance Method Summary collapse
-
#check(prototype_l10n) ⇒ String
Debug method that compares the availible translations against a spesified prototype.
-
#dictionary ⇒ Hash
A copy of the current translation dictionary.
-
#guess_l10n ⇒ String
Try to pick a language based on the Sketchup locale.
-
#initialize(l10n_path, default_l10n = 'en', file_ex = 'l10n') ⇒ Babelfish
constructor
A new instance of Babelfish.
- #inspect ⇒ String
-
#load(l10n) ⇒ Boolean
Loads a translation dictionary.
-
#metadata ⇒ Hash
A copy of the current translation dictionary.
- #translate(string, *args) ⇒ Object (also: #tr)
-
#translations ⇒ Hash
Iterates the language folder for .l10n files and extracts the required display name in the first line of the file.
Constructor Details
#initialize(l10n_path, default_l10n = 'en', file_ex = 'l10n') ⇒ Babelfish
Returns a new instance of Babelfish
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'TT_Lib2/babelfish.rb', line 44 def initialize(l10n_path, default_l10n = 'en', file_ex = 'l10n') @file_ex = file_ex.dup @l10n = default_l10n.dup @default_l10n = default_l10n.dup @dictionary = {} @metadata = {} # (!) Default data path = File.( l10n_path ) unless File.exist?( path ) raise( ArgumentError, 'Path does not exist.' ) end @path = path end |
Instance Attribute Details
#default_l10n ⇒ Object (readonly)
37 38 39 |
# File 'TT_Lib2/babelfish.rb', line 37 def default_l10n @default_l10n end |
#file_ex ⇒ Object (readonly)
37 38 39 |
# File 'TT_Lib2/babelfish.rb', line 37 def file_ex @file_ex end |
#l10n ⇒ Object (readonly)
37 38 39 |
# File 'TT_Lib2/babelfish.rb', line 37 def l10n @l10n end |
#path ⇒ Object (readonly)
37 38 39 |
# File 'TT_Lib2/babelfish.rb', line 37 def path @path end |
Instance Method Details
#check(prototype_l10n) ⇒ String
Debug method that compares the availible translations against a spesified prototype. Outputs the result to the Console.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'TT_Lib2/babelfish.rb', line 260 def check(prototype_l10n) puts "\nChecking language files for missing strings against #{prototype_l10n}..." prototype = self.new( @path, @default_l10n ) prototype.load( prototype_l10n ) temp = self.new( @path, @default_l10n ) keys = prototype.dictionary.keys prototype.translations.each { |code, data| next if code == prototype_l10n temp.load(code) puts "\n=== #{data['name']} (#{code}) ===" missing = keys - temp.dictionary.keys puts "> Missing #{missing.length} keys:" missing.each { |str| p str } } self.load(org) "\nDone\n\n" end |
#dictionary ⇒ Hash
Returns A copy of the current translation dictionary.
74 75 76 |
# File 'TT_Lib2/babelfish.rb', line 74 def dictionary @dictionary.dup end |
#guess_l10n ⇒ String
Try to pick a language based on the Sketchup locale.
-
Tries exact matches.
-
Tries to find by language code, (if SU locale is en-GB) it tries to find “en.l10n”.
-
Tries to find a similar dialect. (if SU locale is en-GB) it will consider “en-US.l10n” a match.
(!) Norwegian is nn-NO and nb-NO, no This appear to be different from how English system works.
msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28VS.80%29.aspx
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'TT_Lib2/babelfish.rb', line 226 def guess_l10n su_locale = Sketchup.get_locale.downcase # Extract list of languages availible file_filter = File.join( @path, "*.#{@file_ex}" ) languages = Dir.glob( file_filter ).map { |filename| File.basename(filename, ".#{@file_ex}").downcase } # First search for exact match for lang_code in languages return lang_code if lang_code == su_locale end # Search for partial match - language code su_country = su_locale.split('-').first for lang_code in languages return lang_code if lang_code == su_country end # Search for partial match - language code and dialect for lang_code in languages country = lang_code.split('-').first return lang_code if country == su_country end # Default return @default_l10n end |
#inspect ⇒ String
61 62 63 64 65 66 67 68 69 |
# File 'TT_Lib2/babelfish.rb', line 61 def inspect if @metadata name = @metadata['name'] size = @dictionary.size "<#{self.class}:#{@l10n} - Speaks #{size} phrases in #{name}>" else "<#{self.class}:#{@l10n}>" end end |
#load(l10n) ⇒ Boolean
Loads a translation dictionary.
93 94 95 96 97 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 124 125 126 127 128 129 130 131 132 133 134 |
# File 'TT_Lib2/babelfish.rb', line 93 def load(l10n) # Special case for default language. if l10n == @default_l10n @l10n = @default_l10n @metadata.clear # (!) Default data @dictionary.clear return true end # Work out the filename and ensure it exists. filename = File.join(@path, "#{l10n}.#{@file_ex}") unless File.exists?(filename) puts "Babelfish - Failed to load l10n: #{l10n} (#{filename}). No such file." return false end # Open the language file and parse the content. Unicode Regex ensures that # UTF-8 files are parsed properly. # # Using a proxy hash prevents a failed appempt of loading a translation # file from ruining the existing translation hash. # # Garbage data - data that doesn't match the expected format is ignored. dictionary = {} = {} File.open(filename, 'r') { |file| = (file) unless puts "Babelfish - Failed to load l10n: #{l10n} (#{filename}). Invalid data." return false end file.each { |line| next if line.match(/^\s*#/u) # Ignore comments next if line.match(/^\s*$/u) # Ignore empty lines if match = line.match(/^\s*"(.*)"\s*=\s*"(.*)"\s*$/u) dictionary[ match[1] ] = match[2] end } } @l10n = l10n.dup @metadata = @dictionary = dictionary true end |
#metadata ⇒ Hash
Returns A copy of the current translation dictionary.
81 82 83 |
# File 'TT_Lib2/babelfish.rb', line 81 def @metadata.dup end |
#translate(string, *args) ⇒ Object Also known as: tr
148 149 150 151 152 153 154 155 |
# File 'TT_Lib2/babelfish.rb', line 148 def translate(string, *args) translated = @dictionary[string] || string sprintf(translated, *args) rescue => e p e. puts e.backtrace.join("\n") sprintf(string, *args) end |
#translations ⇒ Hash
Iterates the language folder for .l10n files and extracts the required display name in the first line of the file.
Returns a hash of all the languages availible. The key is the translation code and the value is a hash with meta data.
The meta data contains one required key: 'name' and optionally 'author' and/or 'contact'.
{
'no' => {
'name' => '...'
},
'fr' => {
'name' => '...',
'author' => '...',
'contact' => '...'
}
}
How to get an array of availible language codes:
codes = babelfish.translations.keys
Silently eats errors and output any errors or warnings to the console.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'TT_Lib2/babelfish.rb', line 186 def translations lang = {} file_filter = File.join(@path, "*.#{@file_ex}") Dir.glob( file_filter ) { |filename| lang_code = File.basename(filename, ".#{@file_ex}") File.open(filename, 'r') { |file| # UTF-8 files might have a BOM mark, account for this. = (file) if lang[lang_code] = else puts "WARNING: No @title in #{filename}" end } } lang # (?) This method appears to return false unless this is here... rescue => e p e. puts e.backtrace.join("\n") ensure lang end |