Class: OrdbokLib::Ordbok

Inherits:
Object
  • Object
show all
Defined in:
modules/ordbok.rb

Overview

Ordbok localization library for SketchUp extensions.

Examples:

# In extension-dir/resources/en-US.lang
# {"greeting":"Hello World!"}

# In your extensions main file:
require "extension-dir/ordbok"
OB = Ordbok.new
OB[:greeting]# => "Hello World!"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Ordbok

Initialize Ordbok object.

It is recommended to assign this object to constant for access throughout your extension.

Examples:

OB = Ordbok.new

Parameters:

  • opts (Hash) (defaults to: {})

    Options for Ordbok object.

Options Hash (opts):

  • :resource_dir (String)

    Absolute path to directory containing language files (defaults to “resources”, relative to where Ordbok.new is called).

  • :remember_lang (Boolean)

    Save language set by #lang= and use same language next time an Ordbok object is created with the same pref_key (defaults to false).

  • :pref_key (Symbol)

    Key to save language setting to, if remember_lang is true (defaults to unique key for each extension, based on parent module names).

  • :lang (Symbol)

    The language to use (defaults to saved preference from previous session (if any and remember_lang is true), what the current SketchUp session uses, en-US, or whatever language is found, in this order).

Raises:

  • (LoadError)

    if no lang files exists in resource_dir.


44
45
46
47
48
49
50
51
52
# File 'modules/ordbok.rb', line 44

def initialize(opts = {})
  @caller_path = caller_locations(1..1).first.path
  @resource_dir = opts.fetch(:resource_dir, default_resource_dir)
  raise LoadError, "No .lang files found in #{@resource_dir}." if available_langs.empty?
  @remember_lang = opts.fetch(:remember_lang, false)
  @pref_key = opts.fetch(:pref_key, default_pref_key)

  try_set_lang(lang_load_queue(opts[:lang] && opts[:lang].to_sym))
end

Instance Attribute Details

#langSymbol

Returns the code of the currently used language.

Returns:

  • (Symbol)

57
58
59
# File 'modules/ordbok.rb', line 57

def lang
  @lang
end

#pref_keySymbol #pref_key=(value) ⇒ Object

Overloads:

  • #pref_keySymbol

    Get the key by witch the language preference is stored between sessions.

    Returns:

    • (Symbol)
  • #pref_key=(value) ⇒ Object

    Set the key by witch the language preference is stored between sessions.

    Parameters:

    • value (Symbol)

73
74
75
# File 'modules/ordbok.rb', line 73

def pref_key
  @pref_key
end

#remember_langBoolean #remember_lang=(value) ⇒ Object

Overloads:

  • #remember_langBoolean

    Get whether the chosen language should be restored in next session.

    Returns:

    • (Boolean)
  • #remember_lang=(value) ⇒ Object

    Set whether the chosen language should be restored in next session.

    Parameters:

    • value (Boolean)

65
66
67
# File 'modules/ordbok.rb', line 65

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.

Examples:

# (Assuming there is a resource directory with valid lang files)
OB = Ordbok.new

# (Assuming :greeting defined as "Hello World!")
OB[:greeting]# => "Hello World!"


# Key can be String too.
OB["greeting"]# => "Hello World!"


# (Assuming :interpolate defined as "Interpolate string here: %{string}.")
OB[:interpolate, string: "Hello World!"]# => "Interpolate string here: Hello World!."


# Keys can be nested, defining groups of related messages.
# For nested nested keys, use String with period as separator.
OB["message_notification.zero"]# => "You have no new messages."


# The :count keyword is not only interpolated to the String, but also
# used to select nested entry (if available). This allows you to
# specify separate strings with different pluralization.

# If the count is 0, the entry :zero is used if available.
# (Assuming "message_notification.zero" is "You have no new message.")
OB["message_notification", count: 0 ]# => "You have no new messages."


# If the count is 1, the entry :one is used if available.
# (Assuming "message_notification.one" is "You have %{count} new message.")
OB["message_notification", count: 1 ]# => "You have 1 new message."


# Otherwise the entry :other is used.
# (Assuming "message_notification.other" is "You have %{count} new messages.")
OB["message_notification", count: 7 ]# => "You have 7 new messages."

Parameters:

  • key (Symbol)
  • opts (Hash) (defaults to: {})

    Interpolation options. See Kernel.format for details.

Options Hash (opts):

  • :count (Numeric)

    The count keyword is not only interpolated to the string, but also used to select nested entry based on pluralization, if available (see example).

Returns:

  • (String)

168
169
170
171
172
173
174
175
176
177
# File 'modules/ordbok.rb', line 168

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_langsArray<Symbol>

List the available languages in the resources directory.

A language is a file with the extension .lang.

Returns:

  • (Array<Symbol>)

80
81
82
83
# File 'modules/ordbok.rb', line 80

def available_langs
  pattern = "#{@resource_dir.tr("\\", "/")}/*.lang"
  Dir.glob(pattern).map { |p| File.basename(p, ".*").to_sym }
end

#inspectObject


85
86
87
# File 'modules/ordbok.rb', line 85

def inspect
  "#<#{self.class.name}:#{self.object_id} (#{lang})>"
end

#lang_available?(lang) ⇒ Boolean

Check if a specific language is available.

Parameters:

  • lang (Symbol)

Returns:

  • (Boolean)

112
113
114
# File 'modules/ordbok.rb', line 112

def lang_available?(lang)
  File.exist?(lang_path(lang))
end

#options_menu(menu) ⇒ Void

Create menu items for use to select language.

Examples:

OB = Ordbok.new(remember_lang: true)
menu = UI.menu("Plugins").add_submenu("My Extension").add_submenu("Language")
OB.options_menu(menu)

Parameters:

  • men (Sketchup::Menu)

Returns:

  • (Void)

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'modules/ordbok.rb', line 189

def options_menu(menu)
  # TODO: Have item for default language (SketchUp Language) followed by a
  # separator.
  # Should ideally call lang= with nil as argument, and have that erase the
  # saved lang option.

  available_langs.sort.each do |lang|
    # TODO: Perhaps use language name set in language file, or localize Ordbok
    # itself with language names, rather than use ISO codes?
    item = menu.add_item(lang.to_s) { self.lang = lang }
    menu.set_validation_proc(item) { self.lang == lang ? MF_CHECKED : MF_UNCHECKED }
  end

  nil
end