Skip to content

Latest commit

 

History

History
203 lines (133 loc) · 4.59 KB

api.md

File metadata and controls

203 lines (133 loc) · 4.59 KB

API References

Global Config

lang

  • Type: String

  • Default: en

  • Usage:

    Get or set a translation language code. Default by en string value.

    Vue.config.lang = 'ja'

fallbackLang

  • Type: String

  • Default: en

  • Usage:

    Get or set a translation fallback language code. Default by en string value.

    Vue.config.fallbackLang = 'ja'

missingHandler

  • Type: Function

  • Default: null

  • Usage:

    Assign a handler for translation missing. The handler gets called with the translation target language, translation key and the Vue instance.

    If mssingHandler is assigned, and occured translation missing, it's not warned.

    Vue.config.missingHandler = function (lang, key, vm) {
      // handle translation missing
    }

i18nFormatter

  • Type: Function

  • Default: null

  • Usage:

    Assign a customer message formatter.

    Vue.config.i18nFormatter = function (string, ...arguments) {
      //...
      //return formattedString;
    }

Global Methods

Vue.locale ( lang, [locale], [cb] )

  • Arguments:

    • {String} lang
    • {Object | Function} [locale]
    • {Function} [cb]
  • Return:

    • locale function or object
  • Usage:

    Register or retrieve a locale

    // register locale with object
    Vue.locale('en', { message: 'hello' })
    
    // register with external locale
    Vue.locale('ja', function () {
      return fetch('/locales/ja', {
        method: 'get',
        // ...
      }).then(function (json) {
        return Promise.resolve(json)
      }).catch(function (error) {
        return Promise.reject()
      })
    }, function () {
      Vue.config.lang = 'ja'
    })

Vue.t( keypath, [lang], [arguments] )

  • Arguments:

    • {String} keypath
    • {String} [lang]
    • {Array | Object [arguments]
  • Return: Translated string

  • Usage: This is the same as the $t method. This is translate function for global locale only. more detail see $t.

Vue.tc( keypath, [choice], [arguments] )

  • Arguments:

    • {String} keypath
    • {Number} [choice] - default: 1
    • {String | Array | Object [arguments]
  • Return: Translated pluralization string

  • Usage: This is the same as the $tc method. This is translate pluralization function for global locale only. more detail see $tc.

Vue.te(keypath, [lang])

  • Arguments:

    • {String} keypath
    • {String} [lang]
  • Return: Whether keypath exists, boolean value

  • Usage: Check whether key path exists in global locale. If you specified lang, check the locale of lang.

Constructor Options

locales

  • Type: Object

  • Details:

    A locale definition object to be made available to the Vue instance only.

  • See also:

Instance Methods

$t(keypath, [lang], [arguments])

  • Arguments:

    • {String} keypath
    • {String} [lang]
    • {Array | Object [arguments]
  • Return: Translated string

  • Usage: Translate the locale of keypath. Translate in preferentially component locale than global locale. If not specified component locale, translate with global locale. If you specified lang, translate the locale of lang. If you specified keypath of list / named formatting local, you must specify arguments too. For arguments more details see Formatting.

$tc( keypath, [choice], [arguments] )

  • Arguments:

    • {String} keypath
    • {Number} [choice] - default: 1
    • {String | Array | Object [arguments]
  • Return: Translated pluralization string

  • Usage:

    Translate the locale of keypath with pluralization. Translate in preferentially component locale than global locale. If not specified component locale, translate with global locale. If you will specify String value to arguments, translate the locale of value. If you wll specify Array or Object value to arguments, you must specify with arguments of $t.

$te(keypath, [lang])

  • Arguments:

    • {String} keypath
    • {String} [lang]
  • Return: Whether keypath exists, boolean value

  • Usage: Check whether key path exists. In Vue instance, If not specified component locale, check with global locale. If you specified lang, check the locale of lang.

Instance Properties

$lang

  • Return The lang you set via Vue.config.lang

  • Usage You can do some complex things if you need. Note a good design should not rely on this too much.