Class: LanguageHandler

Inherits:
Object
  • Object
show all

Overview

The LanguageHandler class contains methods used to help make SketchUp extensions easier to localize across different languages. It looks for translated resources within the Resources folder in the extension's directory structure. All translated resources should be located within the appropriate language folder and encoded in UTF-8. The strings file should include “key”=“value” string pairs in the following format:

Examples:

"String 1"="Localized String 1";
"String 2"="Localized String 2";

Here's an example extension directory structure:

gocorp_swiveldriver.rb <- creates the SketchupExtension instance
gocorp_swiveldriver/
gocorp_swiveldriver/Resources/
gocorp_swiveldriver/Resources/en/swiveldriver.strings <- OPTIONAL localization files by language code
gocorp_swiveldriver/Resources/fr/swiveldriver.strings
gocorp_swiveldriver/Resources/es/swiveldriver.strings

Example code that uses LanguageHandler:

# Create a global language handler object
swiveldriver_langHandler = LanguageHandler.new('swiveldriver.strings')

# Get localized string
localizedStr = swiveldriver_langHandler.GetString('String 1')

Version:

  • SketchUp 2014

Instance Method Summary # collapse

  • #[](key) ⇒ Object

    Looks up and returns the localized version of a given string, based on the language SketchUp is currently running in, and the available translations in the Resources folder.

  • #initialize(filename) ⇒ Object constructor

    The new method is used to create a new LanguageHandler object.

  • #resource_path ⇒ Object

    Returns a string containing the path to the given filename if it can be found in the Resources folder.

  • #strings ⇒ Object

    Returns a Hash object containing the localization dictionary.

Constructor Details

#initialize(filename) ⇒ Object

The new method is used to create a new LanguageHandler object.

Examples:

swiveldriver_langHandler = LanguageHandler.new('swiveldriver.strings')

Returns the new LanguageHandler object

Parameters:

  • filename

    The name of the file that contains the localized strings with their keys in UTF-8 encoding.

Version:

  • SketchUp 2014

Instance Method Details

#[](key) ⇒ Object

Looks up and returns the localized version of a given string, based on the language SketchUp is currently running in, and the available translations in the Resources folder.

Examples:

swiveldriver_langHandler = LanguageHandler.new('swiveldriver.strings')
localized_string = swiveldriver_langHandler['String 1']

Returns the localized string.

Parameters:

  • key

    The key for the string to be retrieved.

Returns:

  • the localized string.

Version:

  • SketchUp 2014

#resource_pathObject

Returns a string containing the path to the given filename if it can be found in the Resources folder.

Examples:

swiveldriver_langHandler = LanguageHandler.new('swiveldriver.strings')
image = swiveldriver_langHandler.resource_path('fancy_image.png')

Returns:

  • path - the location of the file in the Resources folder.

Version:

  • SketchUp 2014

#stringsObject

Returns a Hash object containing the localization dictionary.

Examples:

swiveldriver_langHandler = LanguageHandler.new('swiveldriver.strings')
hash = swiveldriver_langHandler.strings

Returns:

  • hash - the localization dictionary.

Version:

  • SketchUp 2014