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

@accessitech/i18n-redux-toolkit

v1.0.0

Published

An internationalization @reduxjs/toolkit slice package.

Downloads

3

Readme

@accessitech/i18n-redux-toolkit

Internationalization for @reduxjs/tookit projects

This package manages internationalization (i18n) of display text in React apps using ReduxJs Toolkit, and consists of a Redux Toolkit slice, hooks, actions, and helpers.

Usage

  1. Add the redux slice to your store
  2. initialize your translations
  3. Get and Set the language of your App
  4. Use Translations in your App
// store.js

import i18nSlice, { i18nSliceName } from '@accessitech/i18n-redux-toolkit';

export default configureStore({
  reducer: {
    [i18nSliceName]: i18nSlice.reducer,
  },
});
// App.js

import {
  initTranslations,
  getBrowserLanguage,
  setLang,
  useT,
} from '@accessitech/i18n-redux-toolkit';
import translations from './your/local/translations';

store.dispatch(initTranslations(translations));

const App = () => {

  useEffect(() => {
    const browserLang = getBrowserLanguage(translations);
    store.dispatch(setLang(browserLang));
  }, []);

  return <p>{useT('DISPLAY_STRING_KEY')}</p>;
}

export default App;

Requirements

The Redux slice in this module is only compatible with @reduxjs/toolkit, not react-redux.

Translations

This module does not contain any translations itself, and must be provided an object containing Chrome i18n message formatted translations paired with language id keys.

const translations = {
  'en': { ... },
  'es': { ... },
  'etc': { ... },
};

API

Action Creators

initTranslations(translationsLib)

Adds parsed translations library to the store.

/**
 * Initialize Translations
 * @param {object} translationsLib Chrome i18n formatted paired with language id key
 * @returns {object} payload object for the reducer
*/

setLang(lang)

Sets the language in the store.

/**
 * Set Language
 * @param {string} lang Language id key 
 */

Hooks

useCurrentLang()

Get the id key of the current language.

/**
 * Use Current Language
 * @returns {string|null} language key for current language
 */

useLanguageKeys()

Get language keys for loaded translation languages.

/**
 * Use Language Keys
 * @returns {[string]|null} language keys of translated display string libs
 */

useT(stringKey), useT(stringKey, lang)

Translate display strings.

/**
 * Use Translation
 * @param {string} stringKey unique identifier for selecting translated string
 * @param {string} lang (optional) lang code
 * @returns {string|null} translated string or null if lang
 */

useTranslations()

Get translations library object from store.

/**
 * Use Translations
 * @returns {object|null} 
 */

Helpers

parseTranslation(translation)

Get converted display string from i18n formatted objects.

/**
 * Parse Translation
 * @param {*} translation Translation object following Chrome i18n formatting
 * @returns {string} Translated string formatted with placeholders
 */

getBrowserLanguage(translations)

Get users' preferred language from the browser.

/**
 * Get Browser Language
 * @param {*} translations Library of Translations objects 
 * @returns {string} key of preferred language as per window.navigator
 */