# API references
# Extension of Kdu
# Kdu constructor options
# i18n
- Type:
I18nOptions
Component based localization option.
- See also:
KduI18nclass constructor options
# Kdu injected methods
# $t
Arguments:
{Path} key: required{Locale} locale: optional{Array | Object} values: optional
Return:
TranslateResult
Localize the locale message of key. Localize in preferentially component locale messages than global locale messages. If not specified component locale messages, localize with global locale messages. If you specified locale, localize the locale messages of locale. If you specified key of list / named formatting local messages, you must specify values too. For values more details see Formatting.
Tip
Note that you need to guarantee this context equal to component instance in lifecycle methods (e.g. in data options, const $t = this.$t.bind(this)).
# $tc
Arguments:
{Path} key: required{number} choice: optional, default 1{Locale} locale: optional{string | Array | Object} values: optional
Return:
TranslateResult
Localize the locale message of key with pluralization. Localize in preferentially component locale messages than global locale messages. If not specified component locale messages, localize with global locale messages. If you specified locale, localize the locale messages of locale. If you will specify string value to values, localize the locale messages of value. If you will specify Array or Object value to values, you must specify with values of $t.
Tip
Note that you need to guarantee this context equal to component instance in lifecycle methods (e.g. in data options, const $tc = this.$tc.bind(this)).
# $te
Arguments:
{Path} key: required{Locale} locale: optional
Return:
boolean
Check whether key exists. In Kdu instance, If not specified component locale messages, check with global locale messages. If you specified locale, check the locale messages of locale.
Tip
Note that you need to guarantee this context equal to component instance in lifecycle methods (e.g. in data options, const $te = this.$te.bind(this)).
# $d
🆕 7.0+
Arguments:
{number | Date} value: required{Path | Object} key: optional{Locale | Object} locale: optional
Return:
DateTimeFormatResult
Localize the datetime of value with datetime format of key. The datetime format of key need to register to dateTimeFormats option of KduI18n class, and depend on locale option of KduI18n constructor. If you will specify locale argument, it will have priority over locale option of KduI18n constructor.
If the datetime format of key not exist in dateTimeFormats option, fallback to depend on fallbackLocale option of KduI18n constructor.
Tip
Note that you need to guarantee this context equal to component instance in lifecycle methods (e.g. in data options, const $d = this.$d.bind(this)).
# $n
🆕 7.0+
Arguments:
{number} value: required{Path | Object} format: optional{Locale} locale: optional
Return:
NumberFormatResult
Localize the number of value with number format of format. The number format of format need to register to numberFormats option of KduI18n class, and depend on locale option of KduI18n constructor. If you will specify locale argument, it will have priority over locale option of KduI18n constructor.
If the number format of format not exist in numberFormats option, fallback to depend on fallbackLocale option of KduI18n constructor.
If the second format argument specified as an object, it should have the following properties:
key {Path}: optional, number formatlocale {Locale}: optional, localestyle {string}: optional, number format optioncurrency {string}: optional, number format optioncurrencyDisplay {string}: optional, number format optionuseGrouping {string}: optional, number format optionminimumIntegerDigits {string}: optional, number format optionminimumFractionDigits {string}: optional, number format optionmaximumFractionDigits {string}: optional, number format optionminimumSignificantDigits {string}: optional, number format optionmaximumSignificantDigits {string}: optional, number format optionlocaleMatcher {string}: optional, number format optionformatMatcher {string}: optional, number format option
Any specified number format options will have priority over numberFormats of KduI18n constructor.
Tip
Note that you need to guarantee this context equal to component instance in lifecycle methods (e.g. in data options, const $n = this.$n.bind(this)).
# Injected properties
# $i18n
Type:
I18nRead only
Get a KduI18n instance. If you are specify.
If you have specified an i18n option at component options, you will be able to get a KduI18n instance at the component, Otherwise, you will be able get root KduI18n instance.
# KduI18n class
KduI18n class implement I18n interface of flowtype definitions (opens new window)
# Static properties
# version
- Type:
string
kdu-i18n version.
# availabilities
🆕 7.0+
- Type:
IntlAvailability
Whether the following internationalization features are available:
{boolean} dateTimeFormat: locale sensitive datetime formatting{boolean} numberFormat: locale sensitive number formatting
The above internationalization features are depends on the browser environments (opens new window), due to implement with ECMAScript Internationalization API (ECMA-402).
# Constructor options
You can specify the below some options of I18nOptions constructor options of flowtype definitions (opens new window)
# locale
Type:
LocaleDefault:
'en-US'
The locale of localization. If the locale contains a territory and a dialect, this locale contains an implicit fallback.
# fallbackLocale
Type:
FallbackLocaleDefault:
false
The locale of fallback localization. For more complex fallback definitions see fallback.
# messages
Type:
LocaleMessagesDefault:
{}
The locale messages of localization.
# dateTimeFormats
🆕 7.0+
Type:
DateTimeFormatsDefault:
{}
The datetime formats of localization.
- See also:
DateTimeFormatstype of flowtype definitions (opens new window)
# numberFormats
🆕 7.0+
Type:
NumberFormatsDefault:
{}
The number formats of localization.
- See also:
NumberFormatstype of flowtype definitions (opens new window)
# availableLocales
🆕 8.9.0+
Type:
Locale[]Default:
[]Examples:
["en", "ja"]
The list of available locales in messages in lexical order.
# formatter
Type:
FormatterDefault: Built in formatter
The formatter that implemented with Formatter interface.
# modifiers
🆕 8.15.0+
Type:
ModifierDefault:
loweranduppermodifiers
Modifiers functions for linked messages
# missing
Type:
MissingHandlerDefault:
null
A handler for localization missing. The handler gets called with the localization target locale, localization path key, the Kdu instance and values.
If missing handler is assigned, and occurred localization missing, it's not warned.
# fallbackRoot
Type:
BooleanDefault:
true
In the component localization, whether to fall back to root level (global) localization when localization fails.
If false, it's warned, and is returned the key.
# sync
Type:
BooleanDefault:
true
Whether synchronize the root level locale to the component localization locale.
If false, regardless of the root level locale, localize for each component locale.
# silentTranslationWarn
6.1+, 🆙 8.13
Type:
Boolean | RegExpDefault:
false
Whether suppress warnings outputted when localization fails.
If true, suppress localization fail warnings.
If you use regular expression, you can suppress localization fail warnings that it match with translation key (e.g. $t).
# silentFallbackWarn
🆕 8.8+, 🆙 8.13
- Type:
Boolean | RegExp - Default:
false
Whether suppress warnings when falling back to either fallbackLocale or root.
If true, warnings will be generated only when no translation is available at all, and not for fallbacks.
If you use regular expression, you can suppress the fallback warnings that it match key (e.g. $t).
# preserveDirectiveContent
8.7+
Type:
BooleanDefault:
false
Whether k-t directive's element should preserve textContent after directive is unbinded.
# warnHtmlInMessage
8.11+
Type:
WarnHtmlInMessageLevelDefault:
off
Whether to allow the use locale messages of HTML formatting. See the warnHtmlInMessage property.
Important!!
In next major version, warnHtmlInMessage option is warn as default.
# sharedMessages
8.12+
Type:
LocaleMessagesDefault:
undefined
The shared locale messages of localization for components. More detail see Component based localization.
# postTranslation
8.16+
Type:
PostTranslationHandlerDefault:
null
A handler for post processing of translation. The handler gets after being called with the $t, t, $tc, and tc.
This handler is useful if you want to filter on translated text such as space trimming.
# Properties
# locale
Type:
LocaleRead/Write
The locale of localization. If the locale contains a territory and a dialect, this locale contains an implicit fallback.
# fallbackLocale
Type:
FallbackLocaleRead/Write
The locale of fallback localization. For more complex fallback definitions see fallback.
# messages
Type:
LocaleMessagesRead only
The locale messages of localization.
# dateTimeFormats
🆕 7.0+
Type:
DateTimeFormatsRead only
The datetime formats of localization.
# numberFormats
🆕 7.0+
Type:
NumberFormatsRead only
The number formats of localization.
# missing
Type:
MissingHandlerRead/Write
A handler for localization missing.
# formatter
Type:
FormatterRead/Write
The formatter that implemented with Formatter interface.
# silentTranslationWarn
6.1+, 🆙 8.13
Type:
Boolean | RegExpRead/Write
Whether suppress warnings outputted when localization fails.
# silentFallbackWarn
🆕 8.8+, 🆙 8.13
Type:
Boolean | RegExpRead/Write
Whether suppress fallback warnings when localization fails.
# preserveDirectiveContent
8.7+
Type:
BooleanRead/Write
Whether k-t directive's element should preserve textContent after directive is unbinded.
# warnHtmlInMessage
8.11+
Type:
WarnHtmlInMessageLevelRead/Write
Whether to allow the use locale messages of HTML formatting.
If you set warn orerror, will check the locale messages on the KduI18n instance.
If you are specified warn, a warning will be output at console.
If you are specified error will occurred an Error.
In KduI18n instance, set the off as default.
# postTranslation
8.16+
Type:
PostTranslationHandlerRead/Write
A handler for post processing of translation.
# Methods
# getChoiceIndex
Arguments:
{number} choice{number} choicesLength
Return:
finalChoice {number}
Get pluralization index for current pluralizing number and a given amount of choices. Can be overridden through prototype mutation:
KduI18n.prototype.getChoiceIndex = /* custom implementation */
# getLocaleMessage( locale )
Arguments:
{Locale} locale
Return:
LocaleMessageObject
Get the locale message of locale.
# setLocaleMessage( locale, message )
Arguments:
{Locale} locale{LocaleMessageObject} message
Set the locale message of locale.
NOTE
8.11+
If you set warn orerror in the warnHtmlInMessage property, when this method is executed, it will check if HTML formatting is used for locale message.
# mergeLocaleMessage( locale, message )
6.1+
Arguments:
{Locale} locale{LocaleMessageObject} message
Merge the registered locale messages with the locale message of locale.
NOTE
8.11+
If you set warn orerror in the warnHtmlInMessage property, when this method is executed, it will check if HTML formatting is used for locale message.
# t( key, [locale], [values] )
Arguments:
{Path} key: required{Locale} locale: optional{Array | Object} values: optional
Return: :
TranslateResult
This is the same as the Function returned with $t method. More detail see $t.
# tc( key, [choice], [values] )
Arguments:
{Path} key: required{number} choice: optional, default1{string | Array | Object} values: optional
Return:
TranslateResult
This is the same as the Function returned $tc method. More detail see $tc.
# te( key, [locale] )
Arguments:
{string} key: required{Locale} locale: optional
Return:
boolean
Check whether key path exists in global locale message. If you specified locale, check the locale message of locale.
# getDateTimeFormat ( locale )
🆕 7.0+
Arguments:
{Locale} locale
Return:
DateTimeFormat
Get the datetime format of locale.
# setDateTimeFormat ( locale, format )
🆕 7.0+
Arguments:
{Locale} locale{DateTimeFormat} format
Set the datetime format of locale.
# mergeDateTimeFormat ( locale, format )
🆕 7.0+
Arguments:
{Locale} locale{DateTimeFormat} format
Merge the registered datetime formats with the datetime format of locale.
# d( value, [key], [locale] )
🆕 7.0+
Arguments:
{number | Date} value: required{Path | Object} key: optional{Locale | Object} locale: optional
Return:
DateTimeFormatResult
This is the same as $d method of Kdu instance method. More detail see $d.
# getNumberFormat ( locale )
🆕 7.0+
Arguments:
{Locale} locale
Return:
NumberFormat
Get the number format of locale.
# setNumberFormat ( locale, format )
🆕 7.0+
Arguments:
{Locale} locale{NumberFormat} format
Set the number format of locale.
# mergeNumberFormat ( locale, format )
🆕 7.0+
Arguments:
{Locale} locale{NumberFormat} format
Merge the registered number formats with the number format of locale.
# n( value, [format], [locale] )
🆕 7.0+
Arguments:
{number} value: required{Path | Object} format: optional{Locale} locale: optional
Return:
NumberFormatResult
This is the same as $n method of Kdu instance method. More detail see $n.
# Directives
🆕 7.3+
# k-t
Expects:
string | ObjectModifiers:
.preserve: (8.7.0+) preserves elementtextContentwhen directive is unbinded.
Details:
Update the element textContent that localized with locale messages. You can use string syntax or object syntax. string syntax can be specified as a keypath of locale messages. If you can be used object syntax, you need to specify as the object key the following params:
* path: required, key of locale messages
* locale: optional, locale
* args: optional, for list or named formatting
NOTE
The element textContent will be cleared by default when k-t directive is unbinded. This might be undesirable situation when used inside transitions (opens new window). To preserve textContent data after directive unbind use .preserve modifier or global preserveDirectiveContent option.
- Examples:
<!-- string syntax: literal -->
<p k-t="'foo.bar'"></p>
<!-- string syntax: binding via data or computed props -->
<p k-t="msg"></p>
<!-- object syntax: literal -->
<p k-t="{ path: 'hi', locale: 'ja', args: { name: 'nkduy' } }"></p>
<!-- object syntax: binding via data or computed props -->
<p k-t="{ path: greeting, args: { name: fullName } }"></p>
<!-- with preserve modifier -->
<p k-t.preserve="'foo.bar'"></p>
- See also: Custom directive localization
# Components
# i18n functional component
🆕 7.0+
# Props:
path {Path}: required, keypath of locale messageslocale {Locale}: optional, localetag {string|boolean}: optional, default'span'places {Array | Object}: optional (7.2+)
Important!!
In next major version, places prop is deprecated. Please switch to slots syntax.
# Usage:
<div id="app">
<!-- ... -->
<i18n path="term" tag="label" for="tos">
<a :href="url" target="_blank">{{ $t('tos') }}</a>
</i18n>
<!-- ... -->
</div>
const messages = {
en: {
tos: 'Term of Service',
term: 'I accept xxx {0}.'
},
ja: {
tos: '利用規約',
term: '私は xxx の{0}に同意します。'
}
}
const i18n = new KduI18n({
locale: 'en',
messages
})
new Kdu({
i18n,
data: {
url: '/term'
}
}).$mount('#app')
# See also:
# i18n-n functional component
🆕 8.10+
# Props:
value {number}: required, number to formatformat {string | NumberFormatOptions}: optional, number format name or object with explicit format optionslocale {Locale}: optional, localetag {string|boolean}: optional, default'span'
# Usage:
<div id="app">
<!-- ... -->
<i18n-n :value="money" format="currency" tag="label">
<span k-slot:currency="slotProps" class="font-weight: bold">{{ slotProps.currency }}<span>
</i18n-n>
<!-- ... -->
</div>
var numberFormats = {
'en-US': {
currency: {
style: 'currency', currency: 'USD'
}
},
'ja-JP': {
currency: {
style: 'currency', currency: 'JPY'
}
}
}
const i18n = new KduI18n({
locale: 'en-US',
numberFormats
})
new Kdu({
i18n,
data: {
money: 10234,
}
}).$mount('#app')
# Scoped slots
<i18n-n> functional component can accept a number of named scoped slots. List of supported slot names is based on Intl.NumberFormat.formatToParts() output types (opens new window):
currencydecimalfractiongroupinfinityintegerliteralminusSignnanplusSignpercentSign
Each of these named scoped slots will accept three scope parameters:
[slotName] {FormattedNumberPartType}: parameter of the same name as actual slot name (likeinteger)index {Number}: index of the specific part in the array of number partsparts {Array}: array of all formatted number parts
# See also:
# Special Attributes
# place
🆕 7.2+
# Expects: {number | string}
Used on component interpolation to indicate which index of list formatting or key of named formatting.
For detailed usage, see the guide section linked below.