Example #1
0
 app.get('/po/:id', function (req, res) {
     var lang = req.params.id;
     var locale = i18n.localeFrom(lang);
     var jsondata = '';
     try {
         jsondata = po2json.parseSync('locale/' + locale + '/LC_MESSAGES/messages.po');
         res.send (jsondata);
     } catch (e) {
         logger.error(e);
     }
 });
    languages.forEach(function(language) {
      // en-US -> en_US
      var locale = i18n.localeFrom(language);

      // Make the dir for the locale.
      var args = [];

      var filename = path.basename(template, '.pot');
      var outputFile = path.join(baseLocaleDir, locale, 'LC_MESSAGES/' + filename + '.po');

      var cmd = options.cmd || 'msginit';

      // No-op if po already exists.
      if (fs.existsSync(outputFile)) {
        grunt.log.writeln('Locale "' + locale + '" already exists, skipping...');
        return false;
      }

      checkCommand(cmd);

      // Non-interactive :)
      args.push('--no-translator');

      args.push('--input');
      args.push(template);

      args.push('--output');
      args.push(outputFile);

      grunt.file.mkdir(path.join(baseLocaleDir, locale, 'LC_MESSAGES'));

      args.push('-l');
      args.push(locale);

      // Synchronously execute commands.
      runShellSync(cmd, args);

      grunt.log.ok('Locale "' + locale + '" created successfully.');
    });
 destFileName: function (root, language) {
   // items on disk are stored by locale, not language.
   return path.join(root, i18n.localeFrom(language) + '.css');
 }
Example #4
0
 abideObj.localeFrom = function (lang) {
   return abide.localeFrom(lang);
 };
Example #5
0
module.exports = function (config) {

  var abide = require('i18n-abide');

  // Convert the array to an object for faster lookups
  var fontSupportDisabled = config.fonts.unsupportedLanguages
     .reduce(function (prev, val) {
       prev[val] = true;
       return prev;
     }, {});

  // Configure i18n-abide for loading gettext templates.
  // This causes it to process the configuration settings, parse the
  // message files for each language, etc.
  //
  // It actually returns an express application with all that state
  // bundled into a function; we're going to hide that fact with a
  // bit of a wrapper API, returning the function as if it were a
  // stateful object with helper methods.
  var abideMiddleware = abide.abide(
    {
      default_lang: abide.localeFrom(config.defaultLang),
      debug_lang: config.debugLang,
      supported_languages: config.supportedLanguages,
      translation_directory: config.translationDirectory,
      translation_type: config.translationType,
      locale_on_url: true
    }
  );

  // Wrap the abide middleware so we can set fontSupport
  var abideObj = function (req, res, next) {
    // Call the abide middleware with our own `next` function
    // so that we can modify the request object afterward.
    abideMiddleware(req, res, function (val) {
      var lang = abide.normalizeLanguage(req.lang);
      res.locals.fontSupportDisabled = req.fontSupportDisabled = fontSupportDisabled[lang];
      next(val);
    });
  };

  // Export the langaugeFrom() function as-is.
  abideObj.languageFrom = function (locale) {
    return abide.languageFrom(locale);
  };
  abideObj.localeFrom = function (lang) {
    return abide.localeFrom(lang);
  };

  abideObj.normalizeLocale = function (locale) {
    return abide.normalizeLocale(locale);
  };

  // Export the parseAcceptLanguage() function as-is.
  abideObj.parseAcceptLanguage = function (header) {
    return abide.parseAcceptLanguage(header);
  };


  // Export the bestLanguage() function, but using defaults from the config.
  abideObj.bestLanguage = function (accepted, supported) {
    if (!supported) {
      supported = config.supportedLanguages;
    }
    return abide.bestLanguage(accepted, supported, config.defaultLang);
  };

  abideObj.normalizeLanguage = function (lang) {
    return abide.normalizeLanguage(lang);
  };

  // A new function to get a stand-alone 'localization context'
  // This gives us the properties that i18n-abide attaches to the request
  // object, without actually having to be an express app.
  abideObj.localizationContext = function (acceptLang) {
    var fakeReq = {headers: {}, url: ''};
    var fakeResp = {};
    if (acceptLang) {
      fakeReq.headers['accept-language'] = acceptLang;
    }
    var callWasSynchronous = false;
    abideObj(fakeReq, fakeResp, function () { callWasSynchronous = true; });
    if (!callWasSynchronous) {
      throw new Error('uh-oh, the call to i18n-abide was not synchronous!');
    }
    var l10n = {};
    l10n.lang = fakeReq.lang;
    l10n.lang_dir = fakeResp.locals.lang_dir;
    l10n.locale = fakeReq.locale;
    l10n.gettext = fakeReq.gettext.bind(fakeReq);
    l10n.format = fakeReq.format.bind(fakeReq);

    l10n.fontSupportDisabled = fontSupportDisabled[l10n.lang];

    return l10n;
  };


  return abideObj;
};