New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

around-the-world

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

around-the-world

Localization library built on MessageFormat

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

around-the-world

Travis Prettier npm semantic-release License

Simple to use ICU localization library built on MessageFormat

around-the-world is a utility library built on top of MessageFormat that makes it it simple to localize your app.

  • Lazily loads localization templates.
  • Supports custom formatters.
  • Simple API.
  • Dynamically change the locale.

Install

With yarn:

yarn add around-the-world

With npm:

npm install --save around-the-world

Usage

Basics

import aroundTheWorld from 'around-the-world';

(async () => {
  const { localize } = await aroundTheWorld({
    loadLocale: locale => {
      if (locale === 'en-US') {
        return {
          hello_world: 'Hello, world!',
        };
      }
      throw new Error('Unknown locale!');
    },
  });
})();

Loading From a Server

You can easily fetch string tables from your server using the loadLocale function. It must resolve to an object.

const { localize } = await aroundTheWorld({
  loadLocale: async locale => {
    const response = await fetch(`/i18n/${locale}.json`);
    return res.json();
  },
});

localize('hello-world');

Specifying Default Locale

You can specify the default locale to load using defaultLocale. If you don't supply this, navigator.language is used.

const { localize } = await aroundTheWorld({
  loadLocale: locale => { /* ... */ }

  defaultLocale: 'en-AU',
});

Changing the Locale

You can read the current locale at any time by calling getCurrentLocale(), and you can set it by calling setCurrentLocale(). The latter returns a promise that resolves once the locale is loaded.

const { localize, getCurrentLocale, setCurrentLocale } = await aroundTheWorld({
  loadLocale: locale => {
    /* ... */
  },
});

getCurrentLocale(); // 'en-AU'
localize('hello'); // 'Hello'

await setCurrentLocale('jp');
localize('hello'); // 'こんにちは'

Adding Custom Formatters

MessageFormat supports custom formatters, and you can use them with this library!

const { localize } = await aroundTheWorld({
  loadLocale: locale => ({ number: '{value, decimal, 2}' })

  formatters: {
    decimal: (value, locale, arg) => value.toFixed(+arg),
  },
});

localize('number', { value: 12.3456 }); // -> '12.35'

FAQs

Package last updated on 02 Oct 2018

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