npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@artischocke/localiser

v4.0.0

Published

A set of localisation utilities primarily for React applications

Downloads

22

Readme

Localiser

Codecov Coverage NPM version Minified size

A set of localisation utilities designed for React applications.

Today it works only with React Context API (but I'm gonna extend the variety of possible options).

Install

npm install @artischocke/localiser

or, using Yarn:

yarn add @artischocke/localiser

Usage

Suppose you wish to store the configuration in locale.js:

import { initialize } from '@artischocke/localiser';
import enResources from './localeResources/en.json';
import ruResources from './localeResources/ru.json';

const localiser = initialize({
  // Provide locale resources in the object below.
  // You can specify any name you wish.
  localeResources: {
    en: enResources,
    ru: ruResources,
  },
  fallbackLocale: 'en',
});

export default localiser;

Your localisation resources could look like that:

localeResources/en.json

{
  "component/title": "Hello, world",
  "component/copyright": "artischocke (c) 2010 - %currentYear"
}

localeResources/ru.json

{
  "component/title": "Привет, мир",
  "component/copyright": "артисчхоцке (c) 2010 - %currentYear"
}

Note the fact we've got currentYear word here, preceded by % symbol. This is dynamic variable, which can be replaced by any value you provide in l() function received from useLocaliser() hook.

See below for details.

Then, you wrap your core component (App in App.jsx, for example) in LocaleProvider:

import React, { useState } from 'react';
import { LocaleProvider } from '@artischocke/localiser';
import localiser from './locale';

export default function App(props) {
  // You can change locale by any means you wish
  const [locale] = useState('en');

  return (
    <LocaleProvider config={localiser} locale={locale}>
      {/* Your application goes here */}
    </LocaleProvider>
  );
}

Later on, in Component.jsx:

import React from 'react';
import { useLocaliser } from '@artischocke/localiser';

export default function Component() {
  const l = useLocaliser();

  const currentYear = new Date().getFullYear(); // suppose it's 2020 year

  return (
    <div>
      <h1>{l('component/title')}</h1>
      <p>{l('component/copyright', { currentYear })}</p>
    </div>
  );
}

Result:

// locale === 'en'
render(<App />);
// render result:
// <div>
//   <h1>Hello, world</h1>
//   <p>artischocke (c) 2010 - 2020</p>
// </div>

// locale === 'ru'
render(<App />);
// render result:
// <div>
//   <h1>Привет, мир</h1>
//   <p>артисчхоцке (c) 2010 - 2020</p>
// </div>

When we change 'locale' state in App component (e.g. clicking the control button somewhere in the application), our LocaleProvider refreshes its internal knowledge about locale (passed as a prop in the provider), then re-render is invoked, which results to updating the l() return values.

API Reference

initialize(params)

Creates localiser instance with specified parameters.

Arguments

  1. params (required). Type: LocaliserParams.

    Initialisation parameters.

    1. localeResources (required). Type: Record<string, Record<string, string>>.

    2. fallbackLocale. Type: string.

Return value

Localiser instance.

Example

import { initialize } from '@artischocke/localiser';

const enResources = {
  'foo/bar/baz': 'foo bar baz',
  'bax/tax/fax': '%word bax tax fax',
};

const ruResources = {
  'foo/bar/baz': 'фоо бар баз',
  'bax/tax/fax': 'бакс такс факс %word',
};

const localiser = initialize({
  localeResources: {
    en: enResources,
    ru: ruResources,
  },
  fallbackLocale: 'en',
});

export default localiser;

<LocaleProvider />

A higher-order component (HOC) which provides the app's current locale to its children components.

Props

  1. config (required). Type: Localiser.

    You should pass your Localiser instance that you obtained from initialize() function result.

  2. locale (required). Type: string.

    The app's current locale.

Example

import React, { useState } from 'react';
import { LocaleProvider } from '@artischocke/localiser';
import localiser from './locale';

export default function App(props) {
  const [locale, setLocale] = useState('en');

  return (
    <LocaleProvider config={localiser} locale={locale}>
      {/* Your application goes here */}
      <button type="button" onClick={() => setLocale('ru')}>
        Switch to RU
      </button>
      <button type="button" onClick={() => setLocale('en')}>
        Switch to EN
      </button>
    </LocaleProvider>
  );
}

useLocaliser()

A custom hook that returns function which processes its arguments and returns localised string.

Return value

Function l(locKey, params)

  1. locKey (required). Type: string.

    A localisation key which addresses to one of the keys in localisation resource files.

  2. params. Type: Record<string, any>.

    An optional object containing key-value pair(s), where each key represents the same word in the specified locKey text, and each value is a replacement for the key.

Function l() returns string with gathered text (either processed or not). The locale resource object, where the string is collected by the library, is found by the current locale.

If locKey is not found in current locale resources, the method will try to find the key in the fallback locale provided in config. If no luck - the function returns '' (empty string).

Example

import React from 'react';
import { useLocaliser } from '@artischocke/localiser';

export default function Component() {
  const l = useLocaliser();

  const currentYear = new Date().getFullYear(); // suppose it's 2020 year

  return (
    <div>
      <h1>{l('component/title')}</h1>
      <p>{l('component/copyright', { currentYear })}</p>
    </div>
  );
}

// locale === 'en'
// render result:
// <div>
//   <h1>Hello, world</h1>
//   <p>artischocke (c) 2010 - 2020</p>
// </div>

// locale === 'ru'
// render result:
// <div>
//   <h1>Привет, мир</h1>
//   <p>артисчхоцке (c) 2010 - 2020</p>
// </div>

To-do list

  • [ ] Write documentation on GitHub Wiki
  • [x] Implement own Context provider to "unbind" from user's Context
  • [ ] Implement interaction with Redux (as self-sufficient option of the library)
  • [ ] Implement standalone option of the library (no binding to React, Redux, etc.)

Issues

If you find any bugs and other issues when using this library, please let me know about this. Report issues in the specified section.