Class: LanguageManager

mmir. LanguageManager

A class for managing the language of the application.
It's purpose is to load the controllers and their views / partials and provide functions to find controllers or perform actions or helper-actions. This "class" is structured as a singleton - so that only one instance is in use.

Requires

  • module:mmir.Resources
  • module:mmir.CommonUtils
  • module:mmir.SemanticInterpreter

Methods

async,protected_requestGrammar(lang){String}

Request grammar for the provided language. If there is no grammar available for the requested language, no new grammar is set. A grammar is available, if at least one of the following is true for the requested language
  • there exists a JSON grammar file (with correct name and at the correct location)
  • there exists a compiled JavaScript grammar file (with correct name and at the correct location)
Name Type Description
lang String The language of the grammar which should be loaded.
Returns:
Type Description
String The current grammar language
Chooses a language for the application.

The language selection is done as follows:

  1. check if a default language exists
    if it does and if both (!) grammar and dictionary exist for this language, return this language
  2. walk through all languages alphabetically
    1. if for a language both (!) grammar and dictionary exist, return this language memorize the first language with a grammar (do not care, if a dictionary exists)
  3. test if a grammar exists for the default language - do not care about dictionaries - if it does, return the default language
  4. If a language was found (in Step 2.1) return this language
  5. If still no language is returned take the default language if it has a dictionary
  6. If a language exists, take it (the first one)
  7. Take the default language - no matter what
Returns:
Type Description
String The determined language
If a dictionary exists for the given language, true is returned. Else the method returns false.
Name Type Description
lang String the Language String, e.g. "en", "de"
Returns:
Type Description
Boolean True if a dictionary exists for given language.

existsGrammar(lang, grammarType){Boolean}

Checks if either a JSON grammar file or a compiled grammar exists for the given language.
Name Type Description
lang String Language String or grammar ID, e.g. "en", "de"
grammarType "source" | "bin" optional OPTIONAL only check grammar specifications ("source", i.e. JSON grammar), or executable grammar ("bin", i.e. compiled grammar) existence
Returns:
Type Description
Boolean TRUE if a grammar exists for given language (and if grammarType was given, the existing grammar must also match the given grammar type)
If a speech-configuration (file) exists for the given language.
Name Type Description
lang String the language for which existence of the configuration should be checked, e.g. en, de
Returns:
Type Description
Boolean trueif a speech-configuration exists for given language. Otherwise false.

fixLang(providerName, langCode){String}

HELPER for dealing with specific language / country code quirks of speech services: Get the language code for a specific ASR or TTS service, that is if the service requires some specific codes/transformations, then the transformed code is retruned by this function (otherwise the unchanged langCode is returned).
Name Type Description
providerName String corrections for: "nuance" | "mary"
langCode String the original language / country code
Returns:
Type Description
String the (possibly "fixed") language-setting/-code
Gets the default language.
Returns:
Type Description
String The default language
Returns the dictionary of the currently used language.
Returns:
Type Description
Object The JSON object for the dictionary of the currently used language
Gets the language currently used for the translation.
Returns:
Type Description
String The current language

getLanguageConfig(pluginId, feature, separator){String}

Get the language code setting for a specific plugin. Returns the default setting, if no specific setting for the specified plugin was defined.
Name Type Description
pluginId String
feature String | Array.<String> optional OPTIONAL dot-separate path String or "path Array" This parameter may be omitted, if no separator parameter is used. DEFAULT: "language" (the language feature)
separator String optional OPTIONAL the speparator-string that should be used for separating the country-part and the language-part of the code
Returns:
Type Description
String the language-setting/-code
Gets an array of all for the translation available languages.
Returns:
Type Description
String An array of all for the translation available languages
Returns the speech configuration (for ASR/TTS) of the currently used language.
Returns:
Type Description
Object The JSON object for the speech-configuration of the currently used language
Translates a keyword using the current dictionary and returns the translation.
Name Type Description
textVarName String The keyword which should be looked up
Returns:
Type Description
String the translation of the keyword
Name Type Description
lang String optional OPTIONAL a language code for setting the current language and selecting the corresponding language resources
Returns:
Type Description
Promise a deferred promise that gets resolved when the language manager is initialized

setLanguage(lang, doNotLoadResources){String}

Function to set a new language, but only if the new language is different from the current language.

Side Effects

Name Type Description
lang String the new language code
doNotLoadResources Boolean optional OPTIONAL if omitted or TRUTHY will only change the current language, but will not try to load language resources (e.g. dictionary, speech configuration, and grammar). If false will force (re-)loading the language resources, even if lang is the same as the current language.
Returns:
Type Description
String The (new) current language