How to use i18n - 10 common examples

To help you get started, we’ve selected a few i18n examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github apostrophecms / apostrophe / lib / modules / apostrophe-i18n / index.js View on Github external
construct: function(self, options) {

    var i18nOptions = self.options || {};
    _.defaults(i18nOptions, {
      locales: ['en'],
      cookie: 'apos_language',
      directory: self.apos.rootDir + '/locales'
    });
    i18n.configure(i18nOptions);

    // Make the i18n instance available globally in Apostrophe
    console.log('initial');
    self.apos.i18n = i18n;
  }
};
github eshengsky / HostsDock / main.js View on Github external
function initialize() {
    const shouldQuit = makeSingleInstance();
    if (shouldQuit) {
        return app.exit(0);
    }

    i18n.configure({
        locales: localeArray,
        directory: `${__dirname}/locales`,
        objectNotation: true
    });

    function createWindow() {
        const windowOptions = {
            width: 1080,
            minWidth: 680,
            height: 840,
            icon: path.join(__dirname, `${process.platform === 'win32' ? '/public/image/hostsdock.ico' : '/public/image/hostsdock.png'}`),
            show: false
        };

        // Create the browser window.
        mainWindow = new BrowserWindow(windowOptions);
github ladjs / lad / template / helpers / i18n.js View on Github external
indent: '  ',
  api: {
    __: 't',
    __n: 'tn',
    __l: 'tl',
    __h: 'th',
    __mf: 'tmf'
  },
  register: i18n.api,
  defaultLocale: config.defaultLocale,
  syncFiles: config.i18nSyncFiles,
  autoReload: config.i18nAutoReload,
  updateFiles: config.i18nUpdateFiles
});

i18n.translate = function(key, locale) {
  let args = _.values(arguments).slice(2);
  if (_.isUndefined(args)) args = [];
  const phrase = config.i18n[key];
  if (!_.isString(phrase)) {
    logger.warn(`translation key missing: ${key}`);
    return;
  }
  args = [{ phrase, locale }, ...args];
  return i18n.api.t(...args);
};

i18n.middleware = function(ctx, next) {
  // expose api methods to `ctx.req` and `ctx.state`
  i18n.init(ctx.req, ctx.state);

  // override the existing locale detection with our own
github balderdashy / sails / lib / hooks / views / render.js View on Github external
'views': sails.config.paths.views
      }
    });

    // Set up a mock request for i18n to use as context
    var req = {
      headers: {}
    };

    // Initialize i18n if hook is enabled
    if (sails.hooks.i18n) {

      // If a locale was specified as an option, render the view with that locale
      req.headers['accept-language'] = options.locale || sails.hooks.i18n.defaultLocale;

      require('i18n').init(req, options, function() {

        // Set the locale if necessary
        if (options.locale) {
          req.locale = options.locale;
        }

        // Render the view
        sails.config.views.engine.fn(absPathToView, options, cb_view);
      });
    } else {
      sails.config.views.engine.fn(absPathToView, options, cb_view);
    }

  };
};
github Terminal / discordapps.dev / src / discord / handler.js View on Github external
module.exports = async (message, callback) => {
  const mss = {};

  // Set default values
  mss.content = message.content.trim() || '';
  mss.prefix = prefixes.find(prefix => mss.content.toLowerCase().startsWith(prefix)) || '';
  mss.command = '';
  mss.input = '';
  mss.admin = 0;

  i18n.init(message);

  // If there's a prefix, get rid of the prefix and check for any command
  if (mss.prefix && !message.author.bot) {
    const noprefix = mss.content.substring(mss.prefix.length).trim();
    mss.command = Object.keys(commands).find(command => noprefix.startsWith(command)) || '';
    if (mss.command) {
      mss.input = noprefix.substring(mss.command.length).trim();
      mss.cleanInput = clean(message, mss.input);
    }
  }

  // Set the permission level
  /*
   * 3: Administrator of the bot
   * 2: Has administrator role
   * 1: Can kick/ban
github ladjs / lad / template / helpers / i18n.js View on Github external
i18n.middleware = function(ctx, next) {
  // expose api methods to `ctx.req` and `ctx.state`
  i18n.init(ctx.req, ctx.state);

  // override the existing locale detection with our own
  // in order of priority:
  //
  // 1. check the URL, if `/de` then locale is `de`
  // 2. check the cookie
  // 3. check Accept-Language last

  let locale = config.defaultLocale;

  if (_.includes(config.locales, ctx.url.split('/')[1]))
    locale = ctx.url.split('/')[1];
  else if (
    ctx.cookies.get('locale') &&
    _.includes(config.locales, ctx.cookies.get('locale'))
  )
github 7coil / discordmailhooks / discord / handler.js View on Github external
module.exports = async (message) => {
  const mss = {};

  // Set default values
  mss.content = message.content.trim() || '';
  mss.prefix = prefixes.find(prefix => mss.content.toLowerCase().startsWith(prefix)) || '';
  mss.command = '';
  mss.input = '';
  mss.admin = 0;

  i18n.init(message);

  // If there's a prefix, get rid of the prefix and check for any command
  if (mss.prefix && !message.author.bot) {
    const noprefix = mss.content.substring(mss.prefix.length).trim();
    mss.command = Object.keys(commands).find(command => noprefix.startsWith(command)) || '';
    if (mss.command) {
      mss.input = noprefix.substring(mss.command.length).trim();
      mss.cleanInput = clean(message, mss.input);
    }
  }

  if (config.discord.admins.includes(message.author.id)) {
    mss.admin = 3;
  } else if (message.member && message.member.permission.has('administrator')) {
    mss.admin = 2;
  } else if (message.member && message.member.permission.has('manageWebhooks')) {
github SmartThingsCommunity / smartapp-sdk-nodejs / lib / util / smart-app-context.js View on Github external
// For constructing context for proactive API calls not in response to a lifecycle event
			default:
				authToken = data.authToken
				refreshToken = data.refreshToken
				this.executionId = ''
				this.installedAppId = data.installedAppId
				this.locationId = data.locationId
				this.config = data.config
				this.locale = data.locale
				break
		}

		if (app._localizationEnabled) {
			if (this.locale) {
				this.headers = {'accept-language': this.locale}
				i18n.init(this)
			}
		}

		if (authToken) {
			this.api = new SmartThingsApi({
				authToken,
				refreshToken,
				clientId: app._clientId,
				clientSecret: app._clientSecret,
				log: app._log,
				apiUrl: app._apiUrl,
				refreshUrl: app._refreshUrl,
				locationId: this.locationId,
				installedAppId: this.installedAppId,
				contextStore: app._contextStore,
				apiMutex
github 7coil / discordmail / server / discord / handler.js View on Github external
// Set default values
	mss.content = message.content.trim() || '';
	mss.prefix = prefixes.find(prefix => mss.content.toLowerCase().startsWith(prefix)) || '';
	mss.context = null;
	mss.command = '';
	mss.input = '';
	mss.cleanInput = '';
	mss.clean = clean;
	mss.admin = 0;
	mss.banned = null;
	mss.inbox = '0';
	mss.dmail = null;
	mss.ratelimit = 0;

	i18n.init(message);

	// If there's a prefix, get rid of the prefix and check for any command
	if (mss.prefix && !message.author.bot) {
		mss.context = usrPrefix.includes(mss.prefix) ? 'user' : 'guild';
		mss.inbox = mss.context === 'user' ? message.author.id : message.channel.id;
		const noprefix = mss.content.substring(mss.prefix.length).trim();
		mss.command = Object.keys(commands).find(command => noprefix.startsWith(command)) || '';
		if (mss.command) {
			mss.input = noprefix.substring(mss.command.length).trim();
			mss.cleanInput = clean(message, mss.input);

			if (config.get('discord').admins.includes(message.author.id)) {
				mss.admin = 3;
			} else if (message.member && message.member.permission.has('administrator')) {
				mss.admin = 2;
			} else if (message.member && (message.member.permission.has('kickMembers') || message.member.permission.has('banMembers'))) {
github oliversalzburg / i18n-node-angular / i18n-node-routes.js View on Github external
var locale = request.params.locale;
		var phrase = request.params.phrase;

		var result;
		if( request.query.plural ) {
			var singular = phrase;
			var plural = request.query.plural;
			// Make sure the information is added to the catalog if it doesn't exist yet.
			var translated = i18n.__n( {
				singular : singular,
				plural   : plural,
				count    : request.query.count,
				locale   : locale
			} );
			// Retrieve the translation object from the catalog and return it.
			var catalog = i18n.getCatalog( locale );
			result = singular.split( configuration.objectNotation ).reduce( function( object, index ) {
				return object[ index ];
			}, catalog );

		} else {
			result = i18n.__( { phrase : phrase, locale : locale } );
		}
		response.send( result );
	}
};