Scandit.I18n

Methods

(static) getLanguage(): string

Returns device language based on user preferences and system configuration

See:
Returns:
string

Two letter language code

Examples
  • JavaScript
  • ES6
var deviceLanguage = Scandit.I18n.getLanguage();
let deviceLanguage = Scandit.I18n.getLanguage();

(static) loadTranslations(strings: object, defaultLanguage?: string)

Loads translation object with string translations

The default language is a fallback in case the device language is unsupported

Parameters:
Name Type Attributes Description
strings object

Translations

defaultLanguage string <optional>

Two letter language code #

Examples
  • JavaScript
  • ES6
var translations = {
  en: {
    test: 'test-en'
  },
  de: {
    test: 'test-de'
  }
};

Scandit.I18n.loadTranslations(translations);

// Assuming device language is English
Scandit.I18n.translate('test'); // 'test-en'

Scandit.I18n.setLanguage('de');
Scandit.I18n.translate('test'); // 'test-de'

Scandit.I18n.setLanguage('fr');
Scandit.I18n.translate('test'); // 'test-en' - fallback to default language
const translations = {
  en: {
    test: 'test-en'
  },
  de: {
    test: 'test-de'
  }
};

Scandit.I18n.loadTranslations(translations);

// Assuming device language is English
Scandit.I18n.translate('test'); // 'test-en'

Scandit.I18n.setLanguage('de');
Scandit.I18n.translate('test'); // 'test-de'

Scandit.I18n.setLanguage('fr');
Scandit.I18n.translate('test'); // 'test-en' - fallback to default language

(static) setLanguage(language: string)

Sets preferred language for the device user

Parameters:
Name Type Description
language string

Two letter language code

Examples
  • JavaScript
  • ES6
Scandit.I18n.setLanguage('en');
Scandit.I18n.setLanguage('en');

(static) translate(key: string, …arg?: (string | number), options?: object): string

Translates a string.

Arguments can be passed as well, which will be substituted at a given position in the translated string.

Separate translations can be given for singular and plural.

Parameters:
Name Type Attributes Default Description
key string

Translation key

arg string | number <optional>
<repeatable>

Arguments which are available in the string as {1...n}

options object <optional>
{}
Properties
Name Type Attributes Description
count integer <optional>

Number for plural translations

Returns:
string

Translated string

Examples
  • JavaScript
  • ES6
var translations = {
  en: {
    firstExample:                 'First Example',
    exampleWithArguments:         'First argument: {1}, second argument: {2}',
    example_singular:             'One example',
    example_plural:               '{1} examples',
    exampleWithArgument_singular: 'One example, with argument: {2}',
    exampleWithArgument_plural:   '{1} examples, with argument: {2}'
  }
};
Scandit.I18n.loadTranslations(translations);

Scandit.I18n.translate('firstExample');                           // 'First Example'
Scandit.I18n.translate('exampleWithArguments', 'foo', 'bar');     // 'First argument: foo, second argument: bar'
Scandit.I18n.translate('example', {count: 1});                    // 'One example'
Scandit.I18n.translate('example', {count: 2});                    // '2 examples'
Scandit.I18n.translate('exampleWithArgument', 'foo', {count: 1}); // 'One example, with argument: foo'
Scandit.I18n.translate('exampleWithArgument', 'foo', {count: 2}); // '2 examples, with argument: foo'
let translations = {
  en: {
    firstExample:                 'First Example',
    exampleWithArguments:         'First argument: {1}, second argument: {2}',
    example_singular:             'One example',
    example_plural:               '{1} examples',
    exampleWithArgument_singular: 'One example, with argument: {2}',
    exampleWithArgument_plural:   '{1} examples, with argument: {2}'
  }
};
Scandit.I18n.loadTranslations(translations);

Scandit.I18n.translate('firstExample');                           // 'First Example'
Scandit.I18n.translate('exampleWithArguments', 'foo', 'bar');     // 'First argument: foo, second argument: bar'
Scandit.I18n.translate('example', {count: 1});                    // 'One example'
Scandit.I18n.translate('example', {count: 2});                    // '2 examples'
Scandit.I18n.translate('exampleWithArgument', 'foo', {count: 1}); // 'One example, with argument: foo'
Scandit.I18n.translate('exampleWithArgument', 'foo', {count: 2}); // '2 examples, with argument: foo'