Method: MiniGL::Localization.initialize
- Defined in:
- lib/minigl/localization.rb
.initialize ⇒ Object
Initializes the localization system. If you’re using a custom Res.prefix, call this after setting it.
The localization system will look for files with extension ‘.txt’ in the [Res.prefix]/data/text folder. In each file, each string should be specified in one line, with the following format:
identifier content content content...
Use tab characters between the identifier and the text, not white spaces. This makes it easier to make all the texts aligned and is required for the localization system to work. The identifiers will be used as symbols when retrieving strings.
The text contents support placeholders, i.e., markers that can be replaced by arguments you pass to Localization.text. To specify a placeholder, simply use the ‘$’ character. For example, if your string is:
my_string Values: $ and $
the call Localization.text(:my_string, 'test', 10) will result in “Values: test and 10.”
To include a literal ‘$’ in the text, use ‘\$’ (without the quotes). Similarly, use ‘\\’ to represent a literal backslash, and just ‘\’ to represent a line break (i.e. a “\n” in the resulting string).
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/minigl/localization.rb', line 42 def initialize @languages = [] @texts = {} files = Dir["#{Res.prefix}text/*.txt"].sort files.each do |f| lang = f.split('/')[-1].chomp('.txt').to_sym @languages << lang @texts[lang] = {} File.open(f).each do |l| parts = l.split("\t") @texts[lang][parts[0].to_sym] = parts[-1].chomp end end @language = @languages[0] end |