Skip to content

Translations

Modular features a built-in translation system, enabling you to translate your application into multiple languages. This translation system leverages the Laravel Translation System. All translations for your applications can be configured using Laravel's conventions, and can be utilized in your Vue components by invoking a globally available function named __().

Upon installation, the Modular Core Modules publish the translation files to the lang/vendor/modular/{locale} path. You can modify the translations there as needed.

Additionally, you can create new translation files as needed, adhering to Laravel's Translation System conventions, and position them in the lang/{locale}/{locale}.json path. More information on how to create new translation files can be found in this section of the Laravel Documentation.

How to Use

To access a desired translation set in your translation files on your Vue components, follow these steps:

  1. Create a new translation file for your language. For example, to create a translation file for Brazilian Portuguese, create a new file in lang/pt_BR/pt_BR.json with the following content:
json
{
  "Hello": "Olá"
}
  1. In your Vue component, you can access the translation by calling the __() function, passing the translation key as the first argument:
vue
<template>
  <h1>{{ __("Hello") }}</h1>
</template>
  1. In your config/app.php if the locale key is set to pt_BR, the output of your Vue Component will be:
html
<h1>Olá</h1>

If you have dynamic content in your translation, you can pass the dynamic value as the second argument of the __() function, as an object (with key value pairs). In this case you must set the translation key with the : prefix for variables, like this:

json
{
  "Hello :name": "Olá :name"
}

Your Vue component will look like this:

vue
<template>
  <h1>{{ __("Hello", { name: "Daniel" }) }}</h1>
</template>

And the output will be:

html
<h1>Olá Daniel</h1>

Laravel Native Messages Translations

As a bonus, the command php artisan modular:publish-laravel-translations --lang=pt_BR will export the following files to the lang/pt_BR path:

  • auth.php
  • pagination.php
  • passwords.php
  • validation.php

These files, contains the translated messages (from English), for the --lang option, and is actually available for the following languages:

  • pt_BR (Brazilian Portuguese)