Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@skroutz/i18n-webpack-plugin

Package Overview
Dependencies
Maintainers
6
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@skroutz/i18n-webpack-plugin

Embed localization into your bundle

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
6
Weekly downloads
 
Created
Source

i18n Plugin

i18n (localization) plugin for Webpack.

Forked from webpack-contrib/i18n-webpack-plugin.

Installation

npm i -D @skroutz/i18n-webpack-plugin

Or

yarn add @skroutz/i18n-webpack-plugin -D

Usage

This plugin creates bundles with translations baked in. So you can serve the translated bundle to your clients.

plugins: [
  ...
  new I18nPlugin(localization, options)
],
  • localization: object, a key-value map of localization strings
  • options.isDefaultLocale: boolean, is this language the default one

Function reference

For functions that use %{dynamicPart}, you can extend the String prototype with a supplant method, like so:

String.prototype.supplant = function(o) {
  return this.replace(
    /%\{([^{}]*)\}/g,
    (a, b) => {
      const r = o[b];
      return (typeof r === 'string' || typeof r === 'number') ? r : a;
    }
  );
};

Then, you can use the following functions in your code:

__(msgid) Basic translation method

Marks a string as translatable:

__('Hello World!');

To translate a string with dynamic parts:

__('Hello %{username}. Welcome to %{sitename}.').supplant({
  username: User.username,
  sitename: App.sitename
});
n__(*msgid, n) Pluralized translations

To return the singular or plural form depending on how many you have:

n__('shop', 'shops', shop_count);

Additionally, to include the counter in the translated text:

n__('%{stars_count} star', '%{stars_count} stars', Shop.rating.stars).supplant({
  stars_count: Shop.rating.stars
});
s__(msgid) Scoped translations

To provide context to the translation:

s__('Gender|Female');
s__('Formal|Are you sure?');
s__('VolumetricUnit|kg');

License

MIT

FAQs

Package last updated on 04 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc