Does it even work with JSON object messages?
See original GitHub issueI tried:
{
"en": {"Yes": "Yes", "No": "No"}},
"de": {"Yes": "Ja", "No": "Nein"}}
}
window.Lang.setMessages({
'en': en,
'de': de,
});
It parses keys to be flattened like this: de.No
and then, of course, it cannot find the key.
Then I flattened my messages object to be so:
{
"en.Yes": "Yes",
"en.No": "No",
"de.Yes": "Ja",
"de.No": "Nein",
}
window.Lang.setMessages({
'en': en,
'de': de,
});
and it worked.
What is the point of this approach? Why would I need to loop through all my language keys and flatten them?
Since Laravel provided us JSON translations, you should make a configuration option for that.
Lang.prototype._getMessage = function(key, locale) {
locale = locale || this.getLocale();
if (this.messages[locale] === undefined) {
locale = this.getFallback();
}
if (this.messages[locale][key] === undefined) {
locale = this.getFallback();
}
if (this.messages[locale][key] === undefined) {
return null;
}
return this.messages[locale][key];
}
I believe the code above is pretty self-explanatory.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Working with JSON - Learn web development | MDN
JSON is purely a string with a specified data format — it contains only properties, no methods. JSON requires double quotes to be...
Read more >The Complete Guide to Working With JSON - Nylas
JSON is an incredibly popular data format that is easy for humans and computer to read and write. Learn how to take advantage...
Read more >How To Work with JSON in JavaScript - DigitalOcean
This tutorial provides an introduction to working with JSON in JavaScript. Some general use cases of JSON include: storing data, ...
Read more >Working With JSON Data in Python
Get ready. I'm about to show you some real life JSON—just like you'd see out there in the wild. It's okay: JSON is...
Read more >Never confuse JSON and JavaScript Object ever again!
JSON is a lightweight data-interchange format between clients, servers, and within programming languages. It is a subset of the JavaScript ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This snippet is a life saver, finally got it working with
es.json
.Let me share the Laravel-end for future-comers. P.S: I am not sure if this is the most efficient way but works so far 😃
I was also looking for a way to use translation strings as key (https://laravel.com/docs/7.x/localization#using-translation-strings-as-keys). As the OP has explained, the _parseKey seems to not care about this scenario.
So if I have the following language key
es: {'Hello': 'Hola'}
The expected result when I run the following is Lang.get(‘Hello’) is ‘Hola’
But the _parseKey method instead attempts to find the key ‘es.Hello’ which of course does not exist.
Since laravel’s JSON translation files are only 1 level deep, my solution was to alter _getMessage() like the following
This code has worked for me when I load the JSON translation to the root like so:
I am providing an English version of the JSON file as the key itself is the valid English translation.
Added a PR here https://github.com/rmariuzzo/Lang.js/pull/83