项目作者: sergey-demidov

项目描述 :
Automated multilingual text translator for Nuxt i18n module
高级语言: JavaScript
项目地址: git://github.com/sergey-demidov/nuxt-i18n-easy.git
创建时间: 2020-09-12T07:53:49Z
项目社区:https://github.com/sergey-demidov/nuxt-i18n-easy

开源协议:MIT License

下载


nuxt-i18n-easy

npm version
npm downloads
Github Actions CI
Codecov
Codacy Badge
DeepScan grade
License

Automatic multilingual translator for nuxt-i18n

Introduction

The I18n is great.

But what about translating an existing project with hundreds of buttons, prompts and tips in multiple languages?

:scream: Looks intimidating?

:angel: No need to be afraid, nuxt-i18n-easy will save us.

Features

  • Based on nuxt-i18n lazy-load translations
  • Uses existing strings as object properties
  • Recursively scans the source code directories in the project
  • Finds the strings that need translation
  • Translates found strings using Google translate API
  • Checks that the found strings and properties of the i18n config match
  • Writes found values to i18n configuration files
  • Adds syntactic sugar
  • In general, makes life much easier :bath::zzz:

short demo

Setup

1. Add nuxt-i18n-easy module to your project

  1. npm install nuxt-i18n-easy # or yarn add nuxt-i18n-easy

2. Add nuxt-i18n & nuxt-i18n-easy to the modules section of nuxt.config.js

  1. modules: [
  2. 'nuxt-i18n',
  3. 'nuxt-i18n-easy',
  4. ]

3. Add i18n & i18nEasy to the root of nuxt.config.js

  1. export default {
  2. i18n: {
  3. locales: [
  4. {
  5. code: 'en',
  6. name: 'English',
  7. file: 'en.js'
  8. },
  9. {
  10. code: 'ru',
  11. name: 'Русский',
  12. file: 'ru.js'
  13. }
  14. ],
  15. lazy: true,
  16. langDir: 'lang/',
  17. defaultLocale: 'en'
  18. },
  19. i18nEasy: {
  20. directories: [ // default directories for search
  21. './layouts',
  22. './pages',
  23. './components'
  24. ],
  25. files: ['*.vue', '*.js'], // default files
  26. sourceLanguage: 'en', // default source language
  27. },
  28. ...

4. Create a directory i18n.langDir and configuration files

  1. mkdir lang
  2. touch lang/en.js
  3. touch lang/ru.js

5. Buy me a coffee :coffee:


Usage

1. Strings to be translated may be designated as follows:

classic:

  1. $t('Welcome')

with String.prototype extension:

  1. 'Inspire'.tr()

or with directive v-rt

  1. <p v-tr> I will be translated </p>

:sparkles: Remember syntactic sugar, right?

2. Then run the translate script

```shell script
npx translate # or yarn run translate

  1. By default script will process all `i18n.locales` defined in` nuxt.config.js`
  2. To select a specific locale, send its code as the first parameter
  3. ```shell script
  4. npx translate ru # or yarn run translate ru

3. As a result, we will get ready-to-use localization files in the i18n.langDir directory

  1. cat lang/ru.js
  2. export default {
  3. Welcome: 'Добро пожаловать'
  4. Inspire: 'Вдохновлять',
  5. 'I will be translated': 'Я буду переведен',
  6. }

Something needs to be fixed here, but in general this is what we need to start

:trollface: Give it to content managers, and go to rest

4. If for some reason the code parameter is not suitable for specifying the target language, use translationCode instead

  1. i18n: {
  2. locales: [
  3. {
  4. code: 'ua',
  5. name: 'Українська',
  6. file: 'ua.js',
  7. translationCode: uk
  8. },

:u6709: A complete list of supported codes is here

5. Build you project

  1. npm run dev # or yarn run dev
  1. http://localhost:3000/ru

:blue_book: For more details refer to nuxt-i18n module documentation

7. Buy me coffee with doughnut :coffee::doughnut:


Q&A

Q. And what we see in the original localization file?

  1. cat lang/en.js
  2. export default {
  3. Welcome: 'Welcome'
  4. Inspire: 'Inspire',
  5. 'I will be translated': 'I will be translated',
  6. }

:lotus_position: Looks like some kind of Bhagavad Gita… I am that I am… Hoommm… Stop! We need to go on

A. It should be so. This is because we are not using slug variables.

Q. What about the inevitable changes in the source files?

  1. sed -i 's/I will be translated/I will be translated again/' pages/index.vue
  2. npx translate
  3. cat lang/en.js
  4. export default {
  5. Welcome: 'Welcome',
  6. Inspire: 'Inspire',
  7. 'I will be translated': 'I will be translated', // unused
  8. 'I will be translated again': 'I will be translated again'
  9. }
A. As we can see, the key-value pair is in the same place, just marked as unused in the comment

Q. What about data loss in the event of a terrible crash?

  1. ls lang/
  2. en.js en.js.2020-09-10-15-49 ru.js ru.js.2020-09-10-15-49
A. Dont worry, everything is as it should be here too. Before changing the configuration files, a backup is made.

:broom: Dont forget to clean there at least sometimes

Q. Why are you drinking so much coffee?

A. I drink not only coffee. Also, you can buy me a beer :beer::beer:

Google Translation API

By default, the app uses Google translation API V1.
It’s free, but undocumented and has traffic restrictions.
To switch to the official V2 version of the API,
you need to specify your project identifier googleProjectId in i18nEasy section

Development

  • Setup this repository
    1. git clone https://github.com/sergey-demidov/nuxt-i18n-easy.git
    2. cd nuxt-i18n-easy
    3. npm install # or yarn
  • Link it to your amazing nuxt project
    1. npm link
    2. cd ../amazing-nuxt-project
    3. npm link nuxt-i18n-easy
    :nut_and_bolt::hammer: Enjoy

Known Issues

  • The v-tr directive does not support nested tags
  • Google translate API v1 has a limit on the number of requests.

If you see something like this

  1. ERROR Request failed with status code 429

just try again later


If you really read up to here

There, under the hood is one more feature. You may use String.lp() as shortcut to nuxt-i18n‘s localePath()

For example

  1. <nuxt-link :to="'/app/profile'.lp()">Profile</nuxt-link>
  2. // equal to
  3. <nuxt-link :to="localePath('/app/profile')">Profile</nuxt-link>

License

MIT License

Copyright (c) Sergey Demidov sergey.k.demidov@gmail.com