Get 2024 Templates Mega Bundle!$1000 worth of 19 Bootstrap HTML, Vue & React Templates + 3 Vector Sets for just $99
Get for 99$

JS Localization (with i18n?)


I'm having an issue with translating JS files. I haven't been able to find a suitable solution online and the i18n.js file located at public\assets\js\custom\authentication\sign-in\ doesn't seem to be functioning properly. Can you advise me on how to translate strings within general.js?

Thanks


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 (2)


Hey Muhammad Webmaster! I feel your pain with JS localization—I've been there. So, for tackling the translation hurdle in the i18n.js file, here's a trick that worked for me.

First off, double-check if your 'general.js' strings are wrapped with the translation function (usually something like 'translate' or 't'). If not, make sure to add it. Also, confirm that your language files are correctly set up. To check this out try here: https://andersenlab.de/find-developers/java

If the i18n.js isn't playing nice, consider trying a solid library like 'i18next'—it's pretty versatile and might simplify things. Just npm install it, configure, and you're good to go.

And remember, the devil's in the details. Check for typos, file paths, and those pesky commas that can ruin everything. JS can be finicky.

Hope this helps, and good luck untangling the JS translation web!



Hi,

Translation for JavaScript files is not available by default in Laravel. However, you can still achieve translations in JavaScript by manually providing the translation data to your JavaScript files.

To translate strings within a JavaScript file like `general.js` in a Laravel application, you can follow these steps:

1. Create translation functions: In your JavaScript file (`general.js`), define translation functions or methods that retrieve the translations based on the provided keys. You can use the `window.trans` object or a custom translation function to fetch the translations.

Here's an example of a translation function in `general.js` using the `window.trans` object:


function translate(key) {
return window.trans[key] || key;
}


This function checks if a translation for the given key exists in the `window.trans` object. If it exists, it returns the translation; otherwise, it returns the key itself.

2. Use translation functions: Replace the hardcoded strings in `general.js` with the translation functions. Wrap the strings that need translation with the translation function calls.


// Example usage in general.js
const welcomeMessage = translate("messages.welcome");
console.log(welcomeMessage);


The `translate` function will look up the translation for the key `'messages.welcome'` in the `window.trans` object and return the translated string.

3. Populate translation data: In your server-side code (PHP), populate the translation data and pass it to your view or layout file where the JavaScript file is included. You can use Laravel's localization functions like `trans` or `__` to retrieve the translations and pass them as JSON-encoded data.

For example, in your Blade view or layout file:


<script>
window.trans = @json(__("messages"));
</script>


Here, `__('messages')` retrieves the translations for the `'messages'` key and encodes them as JSON. This data is then assigned to the `window.trans` object in the JavaScript context.

Thanks


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  :(