Skip to content

Commit

Permalink
Enable loading of multiple locales and add possibility to switch betw…
Browse files Browse the repository at this point in the history
…een them

The default localization is en-US and it's already included in the library. There's no need
to load it explicitly from en-US.js.

The following localization files have been renamed because their name was invalid:
* no-NB.js → nb-NO.js
* ca-CA.js → ca-ES.js

Closes #492
  • Loading branch information
crissdev committed Jan 18, 2015
1 parent 9f9c060 commit c2d0ec1
Show file tree
Hide file tree
Showing 37 changed files with 562 additions and 913 deletions.
42 changes: 42 additions & 0 deletions Dist/knockout.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,32 @@ kv.validateObservable = function (observable) {
return true;
};
;
var _locales = {};
var _currentLocale;

kv.defineLocale = function(name, values) {
if (name && values) {
_locales[name.toLowerCase()] = values;
return values;
}
return null;
};

kv.locale = function(name) {
if (name) {
name = name.toLowerCase();

if (_locales.hasOwnProperty(name)) {
kv.localize(_locales[name]);
_currentLocale = name;
}
else {
throw new Error('Localization ' + name + ' has not been loaded.');
}
}
return _currentLocale;
};

//quick function to override rule messages
kv.localize = function (msgTranslations) {
var rules = kv.rules;
Expand All @@ -1401,6 +1427,22 @@ kv.localize = function (msgTranslations) {
}
}
};

// Populate default locale (this will make en-US.js somewhat redundant)
(function() {
var localeData = {};
var rules = kv.rules;

for (var ruleName in rules) {
if (rules.hasOwnProperty(ruleName)) {
localeData[ruleName] = rules[ruleName].message;
}
}
kv.defineLocale('en-us', localeData);
})();

// No need to invoke locale because the messages are already defined along with the rules for en-US
_currentLocale = 'en-us';
;/**
* Possible invocations:
* applyBindingsWithValidation(viewModel)
Expand Down
2 changes: 1 addition & 1 deletion Dist/knockout.validation.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Dist/knockout.validation.min.js.map

Large diffs are not rendered by default.

43 changes: 12 additions & 31 deletions Localization/bg-BG.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
/************************************************
* This is an example localization page. All of these
* messages are the default messages for ko.validation
*
* Currently ko.validation does multiple parameter replacement
* on your message (indicated by the {0}, {1}, etc.).
*
* The parameters that you provide in your validation extender
* are what are passed to your message to do the {0}, {1} etc. replacements.
*
* eg: myProperty.extend({ minLength: 5 });
* ... will provide a message of "Please enter at least 5 characters"
* when validated
*
* eg: myProperty.extend({ between: [1, 5] });
* ... will provide a message of "Please enter between 1 and 5 characters"
* when validated
*
* This message replacement obviously only works with primitives
* such as numbers and strings. We do not stringify complex objects
* or anything like that currently.
*/
/**
* Localization file for Bulgarian - Bulgaria (bg-BG)
*/
(function(factory) {
// Module systems magic dance.
/*global require,ko,define*/
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS or Node: hard-coded dependency on "knockout"
factory(require("knockout"));
} else if (typeof define === "function" && define["amd"]) {
// AMD anonymous module with hard-coded dependency on "knockout"
define(["knockout"], factory);
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
// CommonJS or Node: hard-coded dependency on 'knockout'
factory(require('knockout'));
} else if (typeof define === "function" && define['amd']) {
// AMD anonymous module with hard-coded dependency on 'knockout'
define(['knockout'], factory);
} else {
// <script> tag: use the global `ko` object, attaching a `mapping` property
// <script> tag: use the global `ko` object
factory(ko);
}
}(function(ko) {
if (!ko.validation && typeof ko.validation.localize !== 'function') {
if (!ko.validation || typeof ko.validation.defineLocale !== 'function') {
throw new Error('Knockout-Validation is required, please ensure it is loaded before this localization file');
}
ko.validation.localize({
return ko.validation.defineLocale('bg-BG', {
required: 'Моля, въведете стойност.',
min: 'Моля, въведете стойност по-голяма или равна на {0}.',
max: 'Моля, въведете стойност по-малка или равна на {0}.',
Expand Down
58 changes: 0 additions & 58 deletions Localization/ca-CA.js

This file was deleted.

39 changes: 39 additions & 0 deletions Localization/ca-ES.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Localization file for Catalan - Catalan (ca-ES)
*/
(function(factory) {
// Module systems magic dance.
/*global require,ko,define*/
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
// CommonJS or Node: hard-coded dependency on 'knockout'
factory(require('knockout'));
} else if (typeof define === "function" && define['amd']) {
// AMD anonymous module with hard-coded dependency on 'knockout'
define(['knockout'], factory);
} else {
// <script> tag: use the global `ko` object
factory(ko);
}
}(function(ko) {
if (!ko.validation || typeof ko.validation.defineLocale !== 'function') {
throw new Error('Knockout-Validation is required, please ensure it is loaded before this localization file');
}
return ko.validation.defineLocale('ca-ES', {
required: 'Aquest camp es obligatori',
min: 'Introduir un valor igual o major que {0}',
max: 'Introduir un valor menor o igual que {0}',
minLength: 'Ha de tenir un mínim de {0} caràcters',
maxLength: 'No pot tenir mes de {0} caràcters',
pattern: 'Si us plau, comproveu aquest campo',
step: "El valor ha d'incrementar-se en {0}",
email: 'Aquesta no es una adreça de correu electrònic correcta',
date: 'Introduir una data correcta',
dateISO: 'Introduir una data correcta',
number: 'Ha de ser un nombre',
digit: 'Introduir un dígit',
phoneUS: 'Ha de ser un número de telèfon vàlid',
equal: 'Els valors han de ser iguals',
notEqual: 'Elegiu un altre valor',
unique: 'Ha de ser un valor únic'
});
}));
43 changes: 12 additions & 31 deletions Localization/cs-CZ.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
/************************************************
* This is an example localization page. All of these
* messages are the default messages for ko.validation
*
* Currently ko.validation does multiple parameter replacement
* on your message (indicated by the {0}, {1}, etc.).
*
* The parameters that you provide in your validation extender
* are what are passed to your message to do the {0}, {1} etc. replacements.
*
* eg: myProperty.extend({ minLength: 5 });
* ... will provide a message of "Please enter at least 5 characters"
* when validated
*
* eg: myProperty.extend({ between: [1, 5] });
* ... will provide a message of "Please enter between 1 and 5 characters"
* when validated
*
* This message replacement obviously only works with primitives
* such as numbers and strings. We do not stringify complex objects
* or anything like that currently.
*/
/**
* Localization file for Czech - Czech Republic (cs-CZ)
*/
(function(factory) {
// Module systems magic dance.
/*global require,ko,define*/
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS or Node: hard-coded dependency on "knockout"
factory(require("knockout"));
} else if (typeof define === "function" && define["amd"]) {
// AMD anonymous module with hard-coded dependency on "knockout"
define(["knockout"], factory);
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
// CommonJS or Node: hard-coded dependency on 'knockout'
factory(require('knockout'));
} else if (typeof define === "function" && define['amd']) {
// AMD anonymous module with hard-coded dependency on 'knockout'
define(['knockout'], factory);
} else {
// <script> tag: use the global `ko` object, attaching a `mapping` property
// <script> tag: use the global `ko` object
factory(ko);
}
}(function(ko) {
if (!ko.validation && typeof ko.validation.localize !== 'function') {
if (!ko.validation || typeof ko.validation.defineLocale !== 'function') {
throw new Error('Knockout-Validation is required, please ensure it is loaded before this localization file');
}
ko.validation.localize({
return ko.validation.defineLocale('cs-CZ', {
required: 'Toto pole je povinné.',
min: 'Zadejte číslo větší nebo rovné {0}.',
max: 'Zadejte číslo menší nebo rovné {0}.',
Expand Down
44 changes: 13 additions & 31 deletions Localization/da-DK.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
/************************************************
* This is an example localization page. All of these
* messages are the default messages for ko.validation
*
* Currently ko.validation does multiple parameter replacement
* on your message (indicated by the {0}, {1}, etc.).
*
* The parameters that you provide in your validation extender
* are what are passed to your message to do the {0}, {1} etc. replacements.
*
* eg: myProperty.extend({ minLength: 5 });
* ... will provide a message of "Please enter at least 5 characters"
* when validated
*
* eg: myProperty.extend({ between: [1, 5] });
* ... will provide a message of "Please enter between 1 and 5 characters"
* when validated
*
* This message replacement obviously only works with primitives
* such as numbers and strings. We do not stringify complex objects
* or anything like that currently.
*/
/**
* Localization file for Danish - Denmark (da-DK)
*/

(function(factory) {
// Module systems magic dance.
/*global require,ko,define*/
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS or Node: hard-coded dependency on "knockout"
factory(require("knockout"));
} else if (typeof define === "function" && define["amd"]) {
// AMD anonymous module with hard-coded dependency on "knockout"
define(["knockout"], factory);
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
// CommonJS or Node: hard-coded dependency on 'knockout'
factory(require('knockout'));
} else if (typeof define === "function" && define['amd']) {
// AMD anonymous module with hard-coded dependency on 'knockout'
define(['knockout'], factory);
} else {
// <script> tag: use the global `ko` object, attaching a `mapping` property
// <script> tag: use the global `ko` object
factory(ko);
}
}(function(ko) {
if (!ko.validation && typeof ko.validation.localize !== 'function') {
if (!ko.validation || typeof ko.validation.defineLocale !== 'function') {
throw new Error('Knockout-Validation is required, please ensure it is loaded before this localization file');
}
ko.validation.localize({
return ko.validation.defineLocale('da-DK', {
required: 'Dette felt er påkrævet.',
min: 'Angiv en værdi der mindst er {0}.',
max: 'Angiv en værdi der højst er {0}.',
Expand Down
43 changes: 12 additions & 31 deletions Localization/de-DE.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
/************************************************
* This is an example localization page. All of these
* messages are the default messages for ko.validation
*
* Currently ko.validation does multiple parameter replacement
* on your message (indicated by the {0}, {1}, etc.).
*
* The parameters that you provide in your validation extender
* are what are passed to your message to do the {0}, {1} etc. replacements.
*
* eg: myProperty.extend({ minLength: 5 });
* ... will provide a message of "Please enter at least 5 characters"
* when validated
*
* eg: myProperty.extend({ between: [1, 5] });
* ... will provide a message of "Please enter between 1 and 5 characters"
* when validated
*
* This message replacement obviously only works with primitives
* such as numbers and strings. We do not stringify complex objects
* or anything like that currently.
*/
/**
* Localization file for German - Germany (de-DE)
*/
(function(factory) {
// Module systems magic dance.
/*global require,ko,define*/
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS or Node: hard-coded dependency on "knockout"
factory(require("knockout"));
} else if (typeof define === "function" && define["amd"]) {
// AMD anonymous module with hard-coded dependency on "knockout"
define(["knockout"], factory);
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
// CommonJS or Node: hard-coded dependency on 'knockout'
factory(require('knockout'));
} else if (typeof define === "function" && define['amd']) {
// AMD anonymous module with hard-coded dependency on 'knockout'
define(['knockout'], factory);
} else {
// <script> tag: use the global `ko` object, attaching a `mapping` property
// <script> tag: use the global `ko` object
factory(ko);
}
}(function(ko) {
if (!ko.validation && typeof ko.validation.localize !== 'function') {
if (!ko.validation || typeof ko.validation.defineLocale !== 'function') {
throw new Error('Knockout-Validation is required, please ensure it is loaded before this localization file');
}
ko.validation.localize({
return ko.validation.defineLocale('de-DE', {
required: 'Dieses Feld ist erforderlich.',
min: 'Bitte geben Sie einen Wert größer oder gleich {0} ein.',
max: 'Bitte geben Sie einen Wert kleiner oder gleich {0} ein.',
Expand Down
Loading

0 comments on commit c2d0ec1

Please sign in to comment.