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

if there's no matching translation for the current locale then fallba… #62

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions resources/js/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
}(this, function () {

var locale;
var locale, defaultLocale = "en";
var messages = {};

/* Utility functions: */
Expand Down Expand Up @@ -87,8 +87,10 @@
return result;
}

/* if there is nothing to return, return messageKey */
return messageKey;
/* if there is nothing to return, return in default lang */
/* if message not defined in default language then return message key */
var defaultLangMessage = messages[defaultLocale][messageKey];
return typeof defaultLangMessage == "undefined" ? messageKey : defaultLangMessage;
}

var message = messages[uselocale][messageKey];
Expand Down Expand Up @@ -127,12 +129,8 @@
* @return {String} Translated message.
*/
choice: function (messageKey, count, replacements) {
if (typeof messages[locale][messageKey] == "undefined") {
return messageKey;
}

var message;
var messageSplitted = messages[locale][messageKey].split('|');
var messageSplitted = this.get(messageKey).split('|');

if (count == 1) {
message = messageSplitted[0];
Expand All @@ -144,7 +142,7 @@
message = applyReplacements(message, replacements);
}

return message;
return typeof message == "undefined" ? messageKey : message;
},

/**
Expand All @@ -167,6 +165,37 @@
}
},

/**
* Sets the default locale. Normally only used once
* during initialization.
*
* @method setDefaultLocale
* @static
* @param {String} localeId The locale returned by Laravel's Lang::locale().
* @throws {Error} An error is thrown if messages[localeId] is not defined.
*/
setDefaultLocale: function (localeId) {
defaultLocale = localeId;

if (!messages[defaultLocale]) {
throw new Error(
'No messages defined for default locale: "' + defaultLocale + '". ' +
'Did you forget to enable it in the configuration?'
);
}
},

/**
* Returns the default locale.
*
* @method locale
* @static
* @return {String} The current locale.
*/
defaultLocale: function () {
return defaultLocale;
},

/**
* Returns the current locale.
*
Expand Down