Automated multilingual text translator for Nuxt i18n module
Automatic multilingual translator for nuxt-i18n
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.
nuxt-i18n
lazy-load translationsi18n
config matchnuxt-i18n-easy
module to your project
npm install nuxt-i18n-easy # or yarn add nuxt-i18n-easy
nuxt-i18n
& nuxt-i18n-easy
to the modules
section of nuxt.config.js
modules: [
'nuxt-i18n',
'nuxt-i18n-easy',
]
i18n
& i18nEasy
to the root
of nuxt.config.js
export default {
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
mkdir lang
touch lang/en.js
touch lang/ru.js
classic:
$t('Welcome')
with String.prototype
extension:
'Inspire'.tr()
or with directive v-rt
<p v-tr> I will be translated </p>
Remember syntactic sugar, right?
translate
script```shell script
npx translate # or yarn run translate
By default script will process all `i18n.locales` defined in` nuxt.config.js`
To select a specific locale, send its code as the first parameter
```shell script
npx translate ru # or yarn run translate ru
i18n.langDir
directory
cat lang/ru.js
export default {
Welcome: 'Добро пожаловать'
Inspire: 'Вдохновлять',
'I will be translated': 'Я буду переведен',
}
Something needs to be fixed here, but in general this is what we need to start
Give it to content managers, and go to rest
code
parameter is not suitable for specifying the target language, use translationCode
instead
i18n: {
locales: [
{
code: 'ua',
name: 'Українська',
file: 'ua.js',
translationCode: uk
},
A complete list of supported codes is here
npm run dev # or yarn run dev
locale.code
. For example
http://localhost:3000/ru
For more details refer to nuxt-i18n module documentation
cat lang/en.js
export default {
Welcome: 'Welcome'
Inspire: 'Inspire',
'I will be translated': 'I will be translated',
}
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.
sed -i 's/I will be translated/I will be translated again/' pages/index.vue
npx translate
cat lang/en.js
export default {
Welcome: 'Welcome',
Inspire: 'Inspire',
'I will be translated': 'I will be translated', // unused
'I will be translated again': 'I will be translated again'
}
ls lang/
en.js en.js.2020-09-10-15-49 ru.js ru.js.2020-09-10-15-49
Dont forget to clean there at least sometimes
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
git clone https://github.com/sergey-demidov/nuxt-i18n-easy.git
cd nuxt-i18n-easy
npm install # or yarn
npm link
cd ../amazing-nuxt-project
npm link nuxt-i18n-easy
v-tr
directive does not support nested tagsIf you see something like this
ERROR Request failed with status code 429
just try again later
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>
Copyright (c) Sergey Demidov sergey.k.demidov@gmail.com