TypeScript Support
Supported Versions
🆕 9.2+
VueI18n use the resources, which locale messages, datetime formats and number formats. Especially, locale messages can be externalized as i18n resources, such as
json
files, in order to collaborate with translators using the Localization service, and these resources can be imported for collaboration.
In order to achieve a smooth localization workflow in conjunction with the Localization Service, you may want to prevent missing localizations or missing resource definitions for externalized i18n resources. And in development, you may not want to spoil the developer experience by preventing key string mistakes in using the translation function like
$t
.
Type-safe resources with schema
You can support the type-safe resources with resource schema using TypeScript.
Type-safe resources in
createI18n
The following is an example code to define type-safe resources for
messages
defined with
createI18n
option.
Locale messages resource:
Application entrypoint:
The above code defines a type from the
en-US
message resource specified in the
messages
option of
createI18n
. This defined type is the master-like schema for message resources handled with VueI18n. This means that you can define it
as a single source of truth
resource in your application. You can define a type-safe resource in other locales by specifying the type defined as a schema from a message resource as the first argument of the type parameter of
createI18n
.
The second argument of the type parameter of
createI18n
is the locale to handle. With this, type checking is performed for each locale specified in the second argument, based on the type of the resource specified in the first argument. In the above code example,
en-US
and
ja-JP
are specified as the main locale, which is also specified in the
locale
option. If you compile typescript in this state, you will get the following error to check that no
ja-JP
resource is defined in the
messages
option.
If you are using Visual Studio Code as an editor, you can notice that there is a resource definition omission in the editor with the following error before you run the typescript compilation.
Type-safe resources in
useI18n
Type-safe resources can be defined not only with
createI18n
, but also on a per-component basis with
useI18n
used with the Composition API.
In addition to local messages, the resource type definitions can also include datetime formats and number formats.
The following is an example of code that defines type-safe resources for locale messages and number formats on a per-component basis in
useI18n
.
locale mesasges to import in Vue components:
Vue components with type-safe resources:
the above codes, by specifying the defined schema as the first type parameter of
useI18n
, you can use TypeScript to check for undefined resources for locale messages and number formats. Also, by specifying the locale to be defined in the second type parameter, TypeScript can check for undefined locales.
Limitation
- Type safety is not supported for i18n custom blocks in SFC. We'll plan to support it in the future.
-
Currently support for
JSON
format only.
The code described so far is available as example . Let's check it!
Type-Safe supporting APIs
Other APIs support a type parameter that allows you to specify the schema of a resource for type-safe resource manipulation, such as:
-
getLocaleMessage
-
setLocaleMessage
-
mergeLocaleMessage
-
getDateTimeFormat
-
setDateTimeFormat
-
mergeDateTimeFormat
-
getNumberFormat
-
setNumberFormat
-
mergeNumberFormat
For more details, check out these following API documentation pages.
Resource Keys completion supporting
NOTICE
Resource Keys completion can be used if you are using Visual Studio Code
Along with the support for type-safe resource definitions, VueI18n now provides APIs such as
t
and
d
for interpolating resource keys in the Composition API.
The following indicate how to interpolate Resource keys in Visual Studio Code for the local scope Vue component described above.
Support for interpolation of Resource Keys can prevent translation missing.
Use-cases on your project, you may have Vue components that do not use local scope, but use global scope for everything.
For that use case, you can also support interpolation of resource keys by explicitly specifying the schema defined for the global scope in the type parameter of
useI18n
.
define schema for global scope:
Then, just import the defined schema and use it as a type parameter of
useI18n
, as in the following Vue component:
As a result, you can use the interpolation of resource keys in the APIs provided by VueI18n, such as
t
and
n
.
NOTICE
Legacy Mode, and interpolation of Resource Keys of APIs such as
$t
and
$d
, which are injected into Component by
globalInjection: true
of Composition API, require explicitly specifying type parameters.
For more details, see the API documentation. https://vue-i18n.intlify.dev/api/injection.html
Global resource schema type definition
In VueI18n, you can define resource types at the global scope level by using TypeScript feature to extend interfaces.
If your project uses all resources as global scope, it is very convenient to handle type-safe resources easily.
VueI18n provides the following interfaces:
-
DefineLocaleMessage
: Interface to globally define the schema for Locale messages -
DefineDateTimeFormat
: Interface to globally define the schema for Datetime formats -
DefineNumberFormat
: Interface to globally define the schema for Number formats
With using these interfaces and the
declare module
, you can define a global schema for VueI18n.
The following is an example of a global schema defined in
d.ts
:
With using the
declare module
and the interface provided by VueI18n, you can define the schema for global resources.
Previously, when using
createI18n
and
useI18n
with type definitions for global scope resources, it was necessary to specify each as a type parameter. This way, you don't need to do that.
The following is an example with
createI18n
:
The first type parameter of
createI18n
above does not specify the type that is the schema of the resource. The above just specifies a type hint for the
global
property of the i18n instance created by
createI18n
. (If
false
, the type is a
Composer
instance for the Composition API, if
true
, the type is a
VueI18n
instance for the legacy API)
The second type parameter of
createI18n
specifies a type hint for options.
In the case of the
useI18n
case used by Vue components, it looks like this: