nuxt-i18n-easy
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?
😱 Looks intimidating?
👼 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 🛀💤
Setup
nuxt-i18n-easy
module to your project
1. Add npm install nuxt-i18n-easy # or yarn add nuxt-i18n-easy
nuxt-i18n
& nuxt-i18n-easy
to the modules
section of nuxt.config.js
2. Add modules: 'nuxt-i18n' 'nuxt-i18n-easy'
i18n
& i18nEasy
to the root
of nuxt.config.js
3. Add i18n: locales: code: 'en' name: 'English' file: 'en.js' code: 'ru' name: 'Русский' file: 'ru.js' lazy: true langDir: 'lang/' defaultLocale: 'en' i18nEasy: directories: // default directories for search './layouts' './pages' './components' files: '*.vue' '*.js' // default files sourceLanguage: 'en' // default source language ...
i18n.langDir
and configuration files
4. Create a directory mkdir langtouch lang/en.jstouch lang/ru.js
Buy me a coffee ☕️
5.Usage
1. Strings to be translated may be designated as follows:
classic:
with String.prototype
extension:
'Inspire'
or with directive v-rt
<p v-tr> I will be translated </p>
✨ Remember syntactic sugar, right?
translate
script
2. Then run the npx translate # or yarn run translate
By default script will process all i18n.locales
defined innuxt.config.js
To select a specific locale, send its code as the first parameter
npx translate ru # or yarn run translate ru
i18n.langDir
directory
3. As a result, we will get ready-to-use localization files in the cat lang/ru.js export default
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
code
parameter is not suitable for specifying the target language, use translationCode
instead
4. If for some reason the i18n: locales: code: 'ua' name: 'Українська' file: 'ua.js' translationCode: uk
:u6709: A complete list of supported codes is here
5. Build you project
npm run dev # or yarn run dev
locale.code
. For example
6. Launch your favorite browser, follow the link given by NUXT + one of specified http://localhost:3000/ru
📘 For more details refer to nuxt-i18n module documentation
Buy me coffee with doughnut ☕️🍩
7.Q&A
Q. And what we see in the original localization file?
cat lang/en.js export default
: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?
sed -i 's/I will be translated/I will be translated again/' pages/index.vuenpx translatecat lang/en.js export default
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?
ls lang/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?
buy me a beer 🍺🍺
A. I drink not only coffee. Also, you canGoogle 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
git clone https://github.com/sergey-demidov/nuxt-i18n-easy.gitcd nuxt-i18n-easynpm install # or yarn
npm linkcd ../amazing-nuxt-projectnpm link nuxt-i18n-easy
🔩🔨 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
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
<nuxt-link :to="'/app/profile'.lp()">Profile</nuxt-link>// equal to<nuxt-link :to="localePath('/app/profile')">Profile</nuxt-link>
License
Copyright (c) Sergey Demidov sergey.k.demidov@gmail.com