Skip to content

Commit

Permalink
feat: legacy typings (#44)
Browse files Browse the repository at this point in the history
* update core/context

* feat: define legacy typings
  • Loading branch information
kazupon authored May 10, 2020
1 parent ef670a7 commit d59d858
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ function appendBlockToChain(
): unknown {
let follow: unknown = true
for (let i = 0; i < block.length && isBoolean(follow); i++) {
follow = appendLocaleToChain(chain, block[i], blocks)
const locale = block[i]
if (isString(locale)) {
follow = appendLocaleToChain(chain, block[i], blocks)
}
}
return follow
}
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export {
PostTranslationHandler
} from './core'
export * from './core/types'
export { MissingHandler, ComposerOptions, Composer } from './composer'
export {
MissingHandler,
ComposerOptions,
Composer,
CustomBlocks
} from './composer'
export {
TranslateResult,
Choice,
Expand Down
125 changes: 124 additions & 1 deletion src/mixin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComponentOptions, getCurrentInstance } from 'vue'
import { Path } from './path'
import { Locale } from './core/context'
import { Composer, ComposerInternalOptions } from './composer'
import { Composer, ComposerInternalOptions, CustomBlocks } from './composer'
import {
VueI18n,
VueI18nInternal,
Expand All @@ -13,6 +13,129 @@ import {
} from './legacy'
import { I18nInternal } from './i18n'

declare module '@vue/runtime-core' {
interface ComponentCustomOptions {
/**
* VueI18n options
*
* @remarks
* See the {@link VueI18nOptions}
*/
i18n?: VueI18nOptions
/**
* For custom blocks options
* @internal
*/
__i18n?: CustomBlocks
}
interface ComponentCustomProperties {
/**
* VueI18n class compatible interface. See {@link VueI18n}
*
* @remarks
* If you have specified an `i18n` option at component options,
* you will be able to get a VueI18n instance at the component,
* Otherwise, you will be able get root VueI18n instance.
*
* This property is supported for legacy API only
*/
$i18n?: VueI18n
/**
* translation method
*
* @param key - required, type {@link Path}
* @param locale - optional, type {@link Locale}
* @param values - optional, type `Array` or `Object`
* @returns translated string
*
* @remarks
* 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.
*
* This property is supported for legacy API only
*/
$t?: (...args: unknown[]) => TranslateResult
/**
* pluralization method
*
* @param key - required, type {@link Path}
* @param choice - optional, type `number`, default `1`
* @param locale - optional, type {@link Locale}
* @param values - optional, type `string` or `Array` or `Object`
* @returns pluralized string
*
* @remarks
* 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`.
*
* This property is supported for legacy API only
*/
$tc?: (...args: unknown[]) => TranslateResult
/**
* translation exist method
*
* @param key - required, type {@link Path}
* @param locale - optional, type {@link Locale}
* @returns key exsiting result
*
* @remarks
* Check whether key exists.
* In Vue instance, If not specified component locale messages,
* check with global locale messages. If you specified `locale`, check the locale messages of `locale`
*
* This property is supported for legacy API only
*/
$te?: (key: Path, locale?: Locale) => boolean
/**
* datetime method
*
* @param value - required, type `number` or `Date`
* @param key - optional, type {@link Path} or `Object`
* @param locale - optional, type {@link Locale}
* @returns formatted datetime result
*
* @remarks
* Localize the datetime of `value` with datetime format of `key`.
* The datetime format of `key` need to register to `dateTimeFormats` option of {@link VueI18nOptions},
* and depend on `locale` option of {@link VueI18nOptions}.
* If you will specify locale argument, it will have priority over `locale` of {@link VueI18nOptions}.
*
* If the datetime format of `key` not exist in `dateTimeFormats` option,
* fallback to depend on `fallbackLocale` of {@link VueI18nOptions}.
*
* This property is supported for legacy API only
*/
$d?: (...args: unknown[]) => DateTimeFormatResult
/**
* number method
*
* @param value - required, type `number`
* @param format - optional, type {@link Path} or `Object`
* @param locale - optional, type {@link Locale}
* @returns formatted number result
*
* @remarks
* Localize the number of `value` with number format of `format`.
* The number format of `format` need to register to `numberFormats` option of {@link VueI18nOptions},
* and depend on `locale` option of {@link VueI18nOptions}.
* If you will specify `locale` argument, it will have priority over `locale` option of {@link VueI18nOptions}
*
* If the number format of `format` not exist in `numberFormats` option,
* fallback to depend on `fallbackLocale` option of {@link VueI18nOptions}
*
* This property is supported for legacy API only
*/
$n?: (...args: unknown[]) => NumberFormatResult
}
}

// supports compatibility for legacy vue-i18n APIs
export function defineMixin(
legacy: VueI18n & VueI18nInternal,
Expand Down

0 comments on commit d59d858

Please sign in to comment.