Skip to content
Cristian Trifan edited this page Feb 3, 2015 · 3 revisions

If your application needs to display the validation messages in multiple languages, you can take advantage of the localization files available in the localization directory. If you can't find the one you need feel free to submit a PR for it.

Since some applications may be using an AMD loader like RequireJS, others just script tags, or maybe Browserify with npm modules, the content of this page is split in sections for each usage.

Using script tags

Add a reference to the localization files, after Knockout-Validation plugin, and dynamically change the language when needed. In previous versions of the library, loading a localization file would immediately update the validation messages. Starting with version 2.0.0 this must be explicitly made by calling ko.validation.locale and prodide a locale ID.

<script type="text/javascript" src="knockout.validation.js"></script>
<script type="text/javascript" src="el-GR.js"></script>
<script type="text/javascript" src="fr-FR.js"></script>

<script>
    // Change validation messages to be in French
    ko.validation.locale('fr-FR');
</script>

A working example can be found here.

Using RequireJS

Although you may be using a different AMD loader you should still be able to use the localization files. The required steps to make this work is not different than what you would do for other libraries. You just need to make sure the module specified in the localization files, knockout.validation, can be loaded.

Since RequireJS configurations can be very different, instead of dumping something here, I created an example for it, available here.

Using localization files in Node.js

Localization files can also be used in node.js. No module mapping or configuration is required in this case.

// Load Knockout-Validation
var kv = require('knockout.validation');

// Load localization file
require('knockout.validation/localization/ro-RO');

// Switch locale
kv.locale('ro-RO')

console.log(kv.rules.required.message);  //  => 'Acest câmp este obligatoriu.'

Notes

Future releases will attempt to load localization files when they are requested, unless they are already loaded. This will make even easier to switch languages dynamically.