Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Locales (languages)

Ghislain B edited this page Oct 29, 2018 · 14 revisions

Locales are simple sets of language defined in external JSON files (located here: /locales/validation), we can easily add any new language as extra files without affecting the behaviour of Angular-Validation. You could even change displayed language on the fly (note that the error message will be reflected only after field value is re-evaluated). Make sure to include the angular-translate library and configure it, see section Include it in your Project

JSON Locale translation files

The generic translation JSON files are located in /locales/validation.

Examples

// inject the validationService inside your Controller
myApp.controller('Ctrl', function ($scope) {
  // define a key, could be on the fly with a button or a menu link
  var key = 'fr';

  // change the translation language & reload the page to make sure all errors were rendered properly
  $scope.switchLanguage = function (key) {
    $translate.use(key).then(function() {
      $route.reload();
    });
  };

});

Note

To be fully localized, I should add the country code at the end of my JSON filename and then change the suffix on the angular-translate loader method, but this would add an overhead and I prefer to keep it simple as validation messages often looks the same anyway (for example English from UK or US would most probably give similar error messages). If you really do want to be fully localized, then again see the configuration of the TranslateProvider in Include it in your Project

Missing your locale?

If you define a new Language set, please make a pull request and I would be really happy to add them in current project... With support from some community members, the library now support over 12 languages (locale), if your locale is not in the list, then please make a pull request and I would be really happy to merge it... Thank you.

Clone this wiki locally