123456789_123456789_123456789_123456789_123456789_

Class: LanguageHandler

Relationships
Inherits: Object

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:

language code gocorp_swiveldriver/Resources/fr/swiveldriver.strings

gocorp_swiveldriver/Resources/es/swiveldriver.strings

Examples:

Example code that uses LanguageHandler:

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

# Get localized string
localizedStr = swiveldriver_lang_handler.GetString('String 1')
"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

Version:

  • SketchUp 2014

Class Method Summary

Instance Method Summary

  • #[](key) ⇒ String

    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.

  • #resource_path ⇒ String

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

  • #strings ⇒ Hash

    Returns a Hash object containing the localization dictionary.

Constructor Details

.new(filename) ⇒ LanguageHandler

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

Examples:

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

Parameters:

  • filename (String)

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

Version:

  • SketchUp 2014

Instance Method Details

#[](key) ⇒ String

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_lang_handler = LanguageHandler.new('swiveldriver.strings')
localized_string = swiveldriver_lang_handler['String 1']

Parameters:

  • key (String)

    The key for the string to be retrieved.

Returns:

  • (String)

    the localized string.

Version:

  • SketchUp 2014

#resource_pathString

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

Examples:

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

Returns:

  • (String)

    the location of the file in the Resources folder.

Version:

  • SketchUp 2014

#stringsHash

Returns a Hash object containing the localization dictionary.

Examples:

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

Returns:

  • (Hash)

    the localization dictionary.

Version:

  • SketchUp 2014