Class: OrdbokLib::Ordbok
- Inherits:
-
Object
- Object
- OrdbokLib::Ordbok
- Defined in:
- modules/ordbok.rb,
modules/lang_menu.rb
Overview
Period (.) is an illegal character in individual keys, as it is used as delimiter for nested keys.
Ordbok localization library for SketchUp extensions.
Instance Attribute Summary collapse
-
#lang ⇒ Symbol
Returns the code of the currently used language.
-
#lang_pref ⇒ Symbol
readonly
Returns the code of the current lang preference.
- #pref_key ⇒ Object
- #remember_lang ⇒ Object
Instance Method Summary collapse
-
#[](key, opts = {}) ⇒ String
Output localized string for key.
-
#available_lang_names ⇒ Hash{Symbol => String}
List names of available languages in the resources directory indexed by their code.
-
#available_langs ⇒ Array<Symbol>
List the available languages in the resources directory.
-
#initialize(lang: nil, resource_dir: nil, remember_lang: true, pref_key: nil) ⇒ Ordbok
constructor
Initialize Ordbok object.
- #inspect ⇒ Object
-
#lang_available?(lang) ⇒ Boolean
Check if a specific language is available.
-
#lang_menu(menu, offer_system_lang: true, system_lang_name: "System Default") ⇒ Void
Create menu items for selecting language.
Constructor Details
#initialize(lang: nil, resource_dir: nil, remember_lang: true, pref_key: nil) ⇒ Ordbok
Initialize Ordbok object.
It is recommended to assign this object to constant for access throughout your extension.
44 45 46 47 48 49 50 51 52 |
# File 'modules/ordbok.rb', line 44 def initialize(lang: nil, resource_dir: nil, remember_lang: true, pref_key: nil) @caller_path = caller_locations(1..1).first.path @resource_dir = resource_dir || default_resource_dir raise LoadError, "No .lang files found in #{@resource_dir}." if available_langs.empty? @remember_lang = remember_lang @pref_key = pref_key || default_pref_key @lang_pref = (lang && lang.to_sym) || (remember_lang && saved_lang) || nil load_language end |
Instance Attribute Details
#lang ⇒ Symbol
Returns the code of the currently used language.
57 58 59 |
# File 'modules/ordbok.rb', line 57 def lang @lang end |
#lang_pref ⇒ Symbol (readonly)
Returns the code of the current lang preference. If no language has been explicitly chosen, nil is returned. Use lang to get the code of the actually used language.
64 65 66 |
# File 'modules/ordbok.rb', line 64 def lang_pref @lang_pref end |
#pref_key ⇒ Symbol #pref_key=(value) ⇒ Object
80 81 82 |
# File 'modules/ordbok.rb', line 80 def pref_key @pref_key end |
#remember_lang ⇒ Boolean #remember_lang=(value) ⇒ Object
72 73 74 |
# File 'modules/ordbok.rb', line 72 def remember_lang @remember_lang end |
Instance Method Details
#[](key, opts = {}) ⇒ String
Output localized string for key.
Formats string according to additional parameters, if any. If key is missing, warn and return stringified key.
182 183 184 185 186 187 188 189 190 191 |
# File 'modules/ordbok.rb', line 182 def [](key, opts = {}) count = opts[:count] template = lookup(key, count) if template format(template, opts) else warn "key #{key} is missing." key.to_s end end |
#available_lang_names ⇒ Hash{Symbol => String}
List names of available languages in the resources directory indexed by their code.
96 97 98 99 |
# File 'modules/ordbok.rb', line 96 def available_lang_names # No #to_h method in SU2014. Hash[available_langs.map { |l| [l, lang_name(l)] }] end |
#available_langs ⇒ Array<Symbol>
List the available languages in the resources directory.
A language is a file with the extension .lang.
87 88 89 90 |
# File 'modules/ordbok.rb', line 87 def available_langs pattern = "#{@resource_dir.tr('\\', '/')}/*.lang" Dir.glob(pattern).map { |p| File.basename(p, ".*").to_sym } end |
#inspect ⇒ Object
101 102 103 |
# File 'modules/ordbok.rb', line 101 def inspect "#<#{self.class.name}:#{object_id} (#{lang})>" end |
#lang_available?(lang) ⇒ Boolean
Check if a specific language is available.
125 126 127 |
# File 'modules/ordbok.rb', line 125 def lang_available?(lang) File.exist?(lang_path(lang)) end |
#lang_menu(menu, offer_system_lang: true, system_lang_name: "System Default") ⇒ Void
Create menu items for selecting language.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'modules/lang_menu.rb', line 24 def (, offer_system_lang: true, system_lang_name: "System Default") if offer_system_lang item = .add_item(system_lang_name) { self.lang = nil } .set_validation_proc(item) { lang_pref.nil? ? MF_CHECKED : MF_UNCHECKED } .add_separator end available_lang_names.sort.each do |code, name| item = .add_item(name) { self.lang = code } if offer_system_lang .set_validation_proc(item) { lang_pref == code ? MF_CHECKED : MF_UNCHECKED } else .set_validation_proc(item) { self.lang == code ? MF_CHECKED : MF_UNCHECKED } end end nil end |