How to build multilingual Vue.js application
Creating a suitable Vue.js sample to try out Gitloc
If you want to create your project from scratch, then you can use the following step-by-step guide, where we will create a multilingual Vue.js sample project using i18next and Gitloc
Create a Vue Application (more at vuejs.org)
Prerequisites
Familiarity with the command line
Install Node.js version 16.0 or higher
In this section we will introduce how to scaffold a Vue Single Page Application on your local machine. The created project will be using a build setup based on Vite and allow us to use Vue Single-File Components (SFCs).
Make sure you have an up-to-date version of Node.js installed, then run the following command in your command line:
This command will install and execute create-vue, the official Vue project scaffolding tool. You will be presented with prompts for several optional features such as TypeScript and testing support:
If you are unsure about an option, simply choose No
by hitting enter for now. Once the project is created, follow the instructions to install dependencies and start the dev server:
You should now have your first Vue project running! Note that the example components in the generated project are written using the Composition API and <script setup>
, rather than the Options API. Here are some additional tips:
The recommended IDE setup is Visual Studio Code + Volar extension. If you use other editors, check out the IDE support section.
More tooling details, including integration with backend frameworks, are discussed in the Tooling Guide.
To learn more about the underlying build tool Vite, check out the Vite docs.
If you choose to use TypeScript, check out the TypeScript Usage Guide.
When you are ready to ship your app to production, run the following:
This will create a production-ready build of your app in the project's ./dist
directory. Check out the Production Deployment Guide to learn more about shipping your app to production.
Setup i18next (more at i18next.github.io and dev.to)
In our example, we will create a project with 2 languages and separate files for translations.
Install some i18next dependencies:
Add i18n.js
file to the repository:
Import this file in our main.js
file:
Place translations into dedicated .json
files in the public
folder:
Now define a language switcher component:
Add translations usage (more at i18next.com and i18next.github.io)
For the first text we just use a simple test
key to directly invoke the $t
function. The $t
is more or less the same as i18next.t
.
For the second text we will use the attribute bindings withtitle
key form welcome
namespace.
For the third text, we will interpolate subtitle
key form welcome
namespace by using the <i18next>
component and named slots. These can be referenced as {someSlot}
in the translations
Finally, сonnect your local project folder to your new repository on GitHub.
Done! Now you can move to the next step - connect remote repository to Gitloc
Last updated