Code coverage report for lib/localize.js

Statements: 100% (21 / 21)      Branches: 100% (14 / 14)      Functions: 100% (1 / 1)      Lines: 100% (21 / 21)      Ignored: none     

All files » lib/ » localize.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48    1 7 1   6 1   5 1         4           4 4 1 1   4 5 5 5 15         15             4 1   4    
'use strict';
 
module.exports = function(html, options) {
  if (!options.nativeLocale) {
    throw new Error('s18n: missing required option `nativeLocale`.');
  }
  if (!options.locales && !options.locale) {
    throw new Error('s18n: missing required option `locale` or `locales`.');
  }
  if (options.locales && options.locale) {
    throw new Error('s18n: both `locale` and `locales` options are defined.');
  }
 
  // strings must be exactly between delimiters in this set to be localized
  // (this avoids unintentional localization of unrelated strings)
  var delimiters = [
    ['>', '<'],
    ['"', '"'],
    ['\'', '\'']
  ];
 
  var localizations = {};
  if (options.locale) {
    options.locales = {};
    options.locales.tmp = options.locale;
  }
  for (var id in options.locales) {
    localizations[id] = html;
    for (var hash in options.nativeLocale) {
      for (var i = 0; i < delimiters.length; i++) {
        var chunks = localizations[id].split(
          delimiters[i][0] +
          options.nativeLocale[hash] +
          delimiters[i][1]);
 
        localizations[id] = chunks.join(
          delimiters[i][0] +
          options.locales[id][hash] +
          delimiters[i][1]);
      }
    }
  }
  if(options.locale){
    localizations = localizations.tmp;
  }
  return localizations;
};