I'm trying to localize the JS knockout verification plugin, but I need to be able to switch between languages on the fly. In the plugin, the problem is open: https://github.com/Knockout-Contrib/Knockout-Validation/issues/158 , but it is older than 2 years (and still open).
What I'm just trying to do is switch the languages of the verification messages after everything is loaded. Here is an example (can be seen on the fiddle: http://jsfiddle.net/Kikketer/S6j2q/ )
<input data-bind='value: phone' />
<div data-bind="text: phone"></div>
<button type='button' data-bind="click: v">Validate</button>
<button type='button' data-bind='click: switchLanguage'>Switch Language</button>
With the following JS:
ko.validation.configure({
registerExtenders: true
});
ko.validation.localize({required: '**Required'});
var InterviewTwo = function() {
this.phone = ko.observable().extend({required: true});
this.errors = ko.validation.group(this);
this.v = function() {
this.errors.showAllMessages();
};
this.switchLanguage = function() {
ko.validation.localize({required: 'eh... sorta?'});
ko.validation.registerExtenders();
};
};
ko.applyBindings(new InterviewTwo());
I noticed in the knockout code, the getter method for an error always returns the first line of a localization error. How to "reinitialize" error lines?
From the KnockoutJS 736 line:
var errorMsgAccessor = function () {
if (!config.messagesOnModified || isModified) {
return isValid ? null : obsv.error; <<<< obsv.error is always the first error message
} else {
return null;
}
};