Home  >  Article  >  Web Front-end  >  How to achieve multi-language and internationalization in Vue

How to achieve multi-language and internationalization in Vue

王林
王林Original
2023-10-15 08:45:461163browse

How to achieve multi-language and internationalization in Vue

How to achieve multi-language and internationalization in Vue

Introduction:
With the acceleration of globalization and the development of the Internet, more and more Web Applications need to support multiple locales to provide a better user experience. As a modern JavaScript framework, Vue also provides some convenient methods for multi-language and internationalization implementation. This article will introduce how to implement multi-language and internationalization in Vue, and give specific code examples.

1. Internationalization library selection
The first task to achieve multi-language and internationalization in Vue is to choose a suitable internationalization library. Currently, the more commonly used internationalization libraries include vue-i18n and vue-intl.

  1. vue-i18n:
    vue-i18n is an internationalization plug-in for Vue.js. It provides a series of APIs and instructions to facilitate us to achieve multi-language and internationalization in Vue applications. . Has good community support and stable update maintenance.
  2. vue-intl:
    vue-intl is an international plug-in for Vue.js based on Format.js. It provides richer functions and more flexible configuration options. If the project requires more complex internationalization processing, vue-intl is a good choice.

2. Basic configuration and usage
Taking vue-i18n as an example, the following will introduce how to configure and use multi-language and internationalization in Vue.

  1. Installation and configuration:
    First, we need to use npm or yarn to install vue-i18n. In the root directory of the project, execute the following command:

    npm install vue-i18n

    Then, introduce vue-i18n into the main.js file of the project and configure it:

    import Vue from 'vue';
    import VueI18n from 'vue-i18n';
    
    Vue.use(VueI18n);
    
    const i18n = new VueI18n({
      locale: 'zh-CN',
      messages: {
     'zh-CN': require('./locales/zh-CN.json'),
     'en-US': require('./locales/en-US.json'),
      },
    });
    
    new Vue({
      i18n,
    }).$mount('#app');

In the above code, we use the Vue.use() method to register vue-i18n as a Vue plugin. Then, create a VueI18n instance and make some configurations. Among them, locale represents the default locale, and messages represents our language pack.

  1. Creation and use of language packages:
    In the above code, we see the messages object passed in when creating a VueI18n instance. It is a mapping table that contains our language pack. We can store messages objects in separate files, such as zh-CN.json and en-US.json, and introduce them through the require() method. The specific language package content can be defined in the following format:

    // zh-CN.json
    {
      "hello": "你好",
      "world": "世界"
    }
    
    // en-US.json
    {
      "hello": "Hello",
      "world": "World"
    }

    Using multi-language and internationalization in the Vue component, we can display the text content in the language package in the following way:

    <template>
      <div>
     <p>{{ $t('hello') }}</p>
     <p>{{ $t('world') }}</p>
      </div>
    </template>

    In the above code, $t() is a method provided by vue-i18n for translating text. In actual use, we can dynamically switch the language environment according to the user's language preference settings and user selection.

3. Dynamic switching of language environment
In some cases, we may need to dynamically switch the language environment at runtime. vue-i18n provides some methods to achieve this functionality.

  1. Switch locale:
    We can switch the locale in the following ways:

    this.$i18n.locale = 'en-US';

    In the above code, we modify the current locale to ' en-US', which will trigger a re-rendering of the Vue component and display the new language content.

  2. Use the value of the locale language environment:
    We can use this.$i18n.locale in the Vue component to get the value of the current locale.

4. Advanced use of internationalization
In addition to basic text translation, we may also need more complex internationalization processing, such as date formatting, number formatting, currency symbols, etc.

  1. Internationalization of date and time:
    vue-i18n provides date and time formatting functions. We can define the date and time format in the language package and use it in the following ways:

    <template>
      <div>
     <p>{{ $d(new Date(), 'short') }}</p>
     <p>{{ $d(new Date(), 'long') }}</p>
      </div>
    </template>

    In the above code, $d() is a function provided by vue-i18n for formatting date and time. method. Among them, 'short' and 'long' are formatting options for date and time.

  2. Internationalization of numbers and currencies:
    Similarly, we can use vue-i18n to format numbers and currencies. Define the format of numbers and currencies in the language package and use it in the following ways:

    <template>
      <div>
     <p>{{ $n(12345.6789, 'currency') }}</p>
     <p>{{ $n(12345.6789, 'decimal') }}</p>
      </div>
    </template>

    In the above code, $n() is a method provided by vue-i18n for formatting numbers and currencies. Among them, 'currency' is the currency formatting option, and 'decimal' is the decimal formatting option.

Summary:
Multi-language and internationalization are one of the indispensable functions of modern web applications. Vue provides convenient tools and libraries to achieve multi-language and internationalization. In this article, we mainly introduce how to use vue-i18n to achieve multi-language and internationalization, and give some specific code examples. I hope this article can help you implement multi-language and internationalization functions in your Vue project.

The above is the detailed content of How to achieve multi-language and internationalization in Vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn