Get 2024 Templates Mega Bundle!14 Bootstrap, Vue & React Templates + 3 Vector Sets
Get for 99$

Metronic 8 Laravel Javascript Translations


I am developing a project with Metronic 8 and Laravel.
Is it possible to translate .js files with messages that are in English to other languages?

For example:
In the Sign UP form, I have a file: 'assets/js/custom/authentication/sign-up/general.js', whose messages are in English.

The quick solution would be to put in the controller:


if (app()->getLocale() == "fr") {
addJavascriptFile("assets/js/custom/authentication/sign-up/general.fr.js");
} else {
addJavascriptFile("assets/js/custom/authentication/sign-up/general.en.js");
}


But it's not the most elegant solution.
Any more efficient way?


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (1)


One more efficient way to handle translations for JavaScript files in your Metronic 8 Laravel project is to use Laravel's built-in localization features. You can follow these steps:

Organize your JavaScript translations:

Create a directory in your Laravel project to store JavaScript translation files. For example, you can create a folder called resources/js/lang.
Create translation files:

Inside the resources/js/lang directory, create separate JSON files for each language you want to support. For example, en.json for English and fr.json for French.

In these JSON files, define key-value pairs for your translations. For instance, in en.json:

{
"welcome_message": "Welcome to our website!",
"sign_up": {
"button_label": "Sign Up"
}
}

And in fr.json:

{
"welcome_message": "Bienvenue sur notre site Web!",
"sign_up": {
"button_label": "S"inscrire"
}
}

Include JavaScript translations in your Blade view:

In your Blade view (e.g., welcome.blade.php or any other view where you use JavaScript), include the translations using the @lang directive:

<script>
var translations = @json(__("lang.file_name"));
</script>

Replace 'lang.file_name' with the actual language file name (e.g., 'lang.en' for English or 'lang.fr' for French).

Use translations in your JavaScript files:

Now, in your JavaScript files (e.g., general.js), you can access translations using the translations object you defined earlier. For example:

console.log(translations.welcome_message); // Outputs the welcome message in the current language.

This approach allows you to centralize your translations in Laravel, making it easier to manage and switch between languages. You won't need to add conditional statements in your controllers, as Laravel's localization features will handle this for you based on the user's selected language.


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(