with I18N
Demo
Installation
Install the i18next and i18next-vue libraries with an optional backend loader, like i18next-xhr-backend
Install NPM package
npm install i18next i18next-vue i18next-xhr-backendadd it to your main.ts
Start by using the plugin in your main.ts
import i18next from 'i18next';
import I18NextVue from 'i18next-vue';
import { createApp } from 'vue';
createApp(App).use(I18NextVue, { i18next })then add it to your App
Then to finally use translations in Slickgrid-Vue, you must first provide the I18Next instance in your App so that it can be injected in Slickgrid-Vue.
<script setup lang="ts">
import { useTranslation } from 'i18next-vue';
import { provide } from 'vue';
provide('i18next', useTranslation().i18next);
</script>optionally configure i18n loader with assets folder
You can use the optional i18next-http-backend to load JSON files asynchronously. This is just 1 in multiple ways to load translations, just choose whichever ways that best fits your use case.
Class sample
You need to add a translation key via the property headerKey to each column definition, for example: headerKey: 'TITLE'
Note
For the Select Filter, you will use labelKey instead of label. Anytime a translation key will come in play, we will add the word key to the end (hence headerKey, labelKey, more to come...)
Custom Formatter (cell values)
You can define your own custom Formatter by providing the i18n Service into the Formatter and using the .tr() function to translate the cell value.
Using slickgrid-vue Formatters.Translate
Instead of defining a custom formatter over and over, you could also use the built-in slickgrid-vue Formatters.translate. However for the formatter to work, you need to provide the i18n Service instance, you can do so using the params properties which is made to pass any type of data, however you need to pass it with this structure: params: { i18n: i18next } .
Passing i18n in the Grid Options for Formatter
The best and quick way to pass the i18n service is to pass it through the generic params grid option. However make sure that you use the following structure: params: { i18n: i18next } .
Locales
The final step is of course the actual translations. There's multiple ways to copy them to your assets folder. See below for a few ways:
Manually copy the translation keys/values
Manually copy the JSON files to your
assetsfolderFor Vue-CLI, you can modify
vue.jsonfile to copy the JSON files to yourassetsfolder via a copy scripts.You can implement something like the following (I did not test this one, please report back if this is not accurate)
Or modify your
package.jsonand add a script to copy the JSON files to yourassetsfolderinstall NPM packages
cross-envandcopyfiles(npm install copy-files cross-env)add a new script in your
package.jsonrun the below script once with
npm run copy:i18nand you should now have the JSON files in yoursrc/assetsfolder
If you want to manually re-create the translation in your own files, the list of translations that you will need are displayed in the asset locales translation folder (from that file, you need all translations shown before the 'BILLING', the next few ones are for the demo page only).
Last updated