Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not proper translations [locale:strings] for EN locale #92

Open
MitichPavel opened this issue Oct 17, 2023 · 1 comment
Open

Not proper translations [locale:strings] for EN locale #92

MitichPavel opened this issue Oct 17, 2023 · 1 comment

Comments

@MitichPavel
Copy link

Laravel-JS-Localization generated translations issue

If in folder lang are translations in json files, translations generated by Laravel-JS-Localization looks like:

{
   "pl:strings": {
      "English key string": "Polish value string"
   }
}

In this case in Laravel provide helper function __ and as a default value it returns passed argument string.
Example:

  lang.setLocale('pl');
  lang.get('English key string') // return ==> 'English key string'
  
  lang.get('strings.English key string') // return ==> 'Polish value string'
  
  // but if now we set locale to default 'en'
  lang.setLocale('en');
  lang.get('strings.English key string') // return ==> 'strings.English key string'

In folder we have only resources/lang/pl.json because default behavior of Laravel function return key as default value.

Possible solution

Add aditional method __ for getting translations generated by Laravel-JS-Localization.

    /**
     * Get a translation message wrapper function for getting JSON Laravel-JS-Localization generated translations 
     *
     * @param key {string} The key of the message.
     * @param replacements {object} The replacements to be done in the message.
     * @param locale {string} The locale to use, if not passed use the default locale.
     *
     * @return {string} The translation message, if not found the given key.
     */

    Lang.prototype.__ = function(key, replacements, locale) {
        const stringsDomain = "strings";
        const stringsKey = `${stringsDomain}.${key}`;
        const stringTranslation = this.has(stringsKey, locale);
        const translation = this.has(key, locale);

        if (!stringTranslation && !translation) {
            return key;
        }

        if (translation) {
            return this.get(key, replacements, locale);
        }

        return this.get(stringsKey, replacements, locale);
    }
@MitichPavel
Copy link
Author

@rmariuzzo 👋

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant