国际化插件vuex-i18n

vuex-i18n

We are big fans of the awesome vue, vuex and vue-router libraries and were just looking for an easy to use internationalization plugin, employing as much of the "standard library" as possible.

The main difference to other internationalization plugins is the ease of use and support for locales directly with the application or later from the server.

Requirements

  • Vue ^2.0.0
  • Vuex ^2.0.0

Installation

$ npm install vuex-i18n

Setup

The vuex-i18n plugin is intended to be used for applications that use vuex as store and require localized messages. Make sure that both vue and vuex have been loaded beforehand.

The plugin provides a vuex module to store the localization information and translations and a plugin to allow easy access from components.

The plugin does not make any assumption on how you want to load the localization information. It can be loaded on start in your application bundle or dynamically after when the user is switching to a different language.

A corresponding example can be found in the test directory.

// load vue and vuex instance
import Vue from 'vue';
import Vuex from 'vuex';

// load vuex i18n module
import vuexI18n from 'vuex-i18n';

// initialize the vuex store using the vuex module. note that you can change the
//  name of the module if you wish
const store = new Vuex.Store({
    modules: {
        i18n: vuexI18n.store
    }
});

// initialize the internationalization plugin on the vue instance. note that
// the store must be passed to the plugin. the plugin will then generate some
// helper functions for components (i.e. this.$i18n.set, this.$t) and on the vue
// instance (i.e. Vue.i18n.set).
Vue.use(vuexI18n.plugin, store);

// please note that you must specify the name of the vuex module if it is
// different from i18n. i.e. Vue.use(vuexI18n.plugin, store, 'myName')


// add some translations (could also be loaded from a separate file)
// note that it is possible to use placeholders. translations can also be
// structured as object trees and will automatically be flattened by the the
// plugin
const translationsEn = {
    "content": "This is some {type} content"
};

// translations can be kept in separate files for each language
// i.e. resources/i18n/de.json.
const translationsDe = {
    "My nice title": "Ein schöner Titel",
    "content": "Dies ist ein toller Inhalt"
};

// add translations directly to the application
Vue.i18n.add('en', translationsEn);
Vue.i18n.add('de', translationsDe);

// set the start locale to use
Vue.i18n.set('en');

// create a new component (requires a div with id app as mount point)
// you can use the method $t to access translations. the value will be returned
// as is, if no corresponding key is found in the translations
var app = new Vue({
    store,
    el: '#app',
    template: `
        <div>
            <h1>{{ 'My nice title' | translate }}</h1>
            <p>{{ $t('content', {'type': 'nice'}) }}
        </div>
    `
});

Usage

The plugin provides easy access to localized information through the use of the $t() method or the translate filter.

The plugin will try to find the given string as key in the translations of the currently defined locale and return the respective translation. If the string is not found, it will return as is. This allows to setup an application very quickly without having to first define all strings in a separate template.

<div>
    // will return: "Some localized information"
    {{ $t('Some localized information')}}
</div>

The $t() method also allows for inclusion of dynamic parameters that can be passed to the method in the form of key/value pairs

<div>
    // will return: "You have 5 new messages"
    {$t('You have {number} new messages', {number: 5}) }}
</div>

There are also several methods available on the property this.$i18n or Vue.i18n

$i18n.locale(), Vue.i18n.locale()       
    // get the current locale

$i18n.set(locale), Vue.i18n.set(locale)
    // set the current locale (i.e. 'de', 'en')

$i18n.add(locale, translations), Vue.i18n.add(locale, translations)
    // add a new locale to the storage
    // (i.e. 'de', {'message': 'Eine Nachricht'})

$i18n.exists(locale), Vue.i18n.exists(locale)
    // check if the given locale translations are present in the store

$i18n.remove(locale), Vue.i18n.remove(locale)
    // remove the given locale from the store
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: vite-plugin-vue-i18n是一个为Vue应用提供国际化i18n)支持的Vite插件。Vite是一个现代化的前端构建工具,而vue-i18nVue官方推荐的国际化解决方案之一。 vite-plugin-vue-i18n主要提供了以下功能: 1. 支持多语言插件可以帮助我们将Vue应用中的文本内容翻译成多种语言,使得应用能够根据用户的地理位置或语言偏好来显示相应的语言版本。 2. 动态加载:插件可以根据需要动态加载所需的语言包,避免了一次性加载所有语言资源的性能问题,提高了应用的加载速度。 3. 热重载:插件支持在开发模式下的热重载,即当我们修改了语言资源文件时,页面会自动刷新以显示最新的翻译内容,提高了开发效率。 4. 构建优化:插件可以使用Vite的优化能力,将应用中的多语言资源进行压缩和合并,减少网络请求,从而提高应用的性能。 使用vite-plugin-vue-i18n,我们可以在Vue应用中轻松地实现国际化需求。通过定义一系列的翻译文件,我们可以将应用中的文本内容翻译成多种语言。在Vue组件中,我们可以使用特定的指令或方法来访问和显示对应的翻译内容。 总之,vite-plugin-vue-i18n为我们提供了一种简洁高效的方式来实现Vue应用的国际化需求,帮助我们更好地面向全球用户开发应用。 ### 回答2: vite-plugin-vue-i18n 是一个用于在 Vite.js 项目中支持国际化插件。Vite.js 是一个基于浏览器原生 ES 模块导入的前端构建工具,vite-plugin-vue-i18n 则是为了在 Vite.js 项目中实现国际化而开发的插件。 这个插件的主要功能是将 Vue 应用程序中的文本国际化,使开发人员能够轻松地在应用中切换和管理多种语言。它能够处理不同语言之间的文本翻译,提供了一种方式来管理和导入不同语言的翻译文件。同时,它还提供了一些工具和指令,使开发人员能够更方便地在应用程序中使用和显示多语言文本。 使用 vite-plugin-vue-i18n,开发人员可以在 Vue 组件中使用特定的指令来标记需要翻译的文本,并通过导入翻译文件来实现文本的动态切换。它支持多种文件格式,如 JSON、YAML、PO 等,可以灵活选择适合自己项目的翻译文件格式。开发人员还可以通过 API 来访问、处理和修改翻译文本。 这个插件还提供了一些其他功能,如支持 Vue 3 的 Composition API、语言切换的路由钩子、翻译文件的合并和拆分等。它还支持在开发环境和生产环境下的不同配置,并具有 TypeScript 类型支持。此外,它还可以与其他插件和构建工具无缝集成,如 VuexVue Router、Vuepress 等。 总的来说,vite-plugin-vue-i18n 是一个功能强大且易于使用的插件,可以帮助开发人员快速实现 Vue 应用程序的国际化。无论是小型项目还是大型项目,都可以借助这个插件来管理多语言文本,提供更好的用户体验。 ### 回答3: vite-plugin-vue-i18n是一个适用于Vite构建工具的插件,用于集成和管理Vue.js应用程序的国际化i18n)功能。它提供了一种简单的方式来处理多语言翻译和本地化,以便应用程序可以方便地在不同的语言环境中展示。 使用vite-plugin-vue-i18n,我们可以在Vue应用程序中轻松地定义多个语言版本的翻译文件,并在运行时根据需要加载适当的翻译信息。我们可以在单个文件中定义所有语言的翻译内容,或者根据语言划分翻译成多个文件进行管理。 该插件提供了一个全局的翻译函数$t,可以在Vue组件中使用它来获取当前语言的翻译文本。我们可以通过在模板中使用`{{ $t('key') }}`的方式来引用翻译内容。而且,我们还可以根据具体的语言环境,动态切换显示的语言版本。 vite-plugin-vue-i18n还支持自动提取模板中的翻译文本,并生成对应的翻译文件。这样我们就可以通过一个命令将应用程序中的所有翻译文本提取出来,然后交给翻译团队进行翻译,最后再将翻译结果导入到应用程序中。 总之,vite-plugin-vue-i18n是一个方便易用的工具,使得开发多语言应用程序变得更加简单和高效。它提供了一种集成且高度可定制的方式来实现应用程序的国际化需求,可以帮助我们轻松地处理不同语言环境下的文本翻译和本地化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值