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

use-mini18n

v0.0.9

Published

A simple and minimalistic React hook library for i18n.

Downloads

5

Readme

use-mini18n

Build Status

use-mini18n - logo image

Install

npm insatll use-mini18n
# or
yarn add use-mini18n

Usage

This is an example using Next.js.

See also examples.

// pages/_app.jsx
import { TransProvider } from 'use-mini18n';
import i18n from '../i18n';

export default function MyApp({ Component, pageProps }) {
  return (
    <TransProvider i18n={i18n}>
      <Component {...pageProps} />
    </TransProvider>
  );
}

This sets the language information required for i18n.js.

// i18n.js
const i18n = {
  /*
   * The first language will be set as the default language.
   * However, it is also possible to set the default language by specifying defaultLang.
   */
  en: {
    'Hello Next.js': 'Hello Next.js',
    'Hello someone': 'Hello {name1} and {name2}',
  },
  ja: {
    'Hello Next.js': 'こんにちは Next.js',
    'Hello someone': 'こんにちは {name1} と {name2}',
  },
};

export default i18n;

Use useI18n.

  • t refers to text with selected language.
  • lang returns the currently selected language.
  • langs returns a list of language information set in i18n.js.
  • Change to another language using the changeLang function.
  • Get the text dynamically by passing set parameters and text keys to the getText function.
// pages/index.jsx
import Layout from '../components/Layout';
import { useI18n } from 'use-mini18n';

const IndexPage = () => {
  const { t, lang, langs, changeLang, getText } = useI18n();

  return (
    <Layout title="Home | Next.js + TypeScript Example">
      <h1>{t['Hello Next.js']} 👋</h1>
      <h2>
        {getText('Hello someone', { name1: 'TestUser1', name2: 'TestUser2' })}
      </h2>
      <p>Selected Language: {lang}</p>
      <select
        name="lang"
        onChange={({ target }) => changeLang(target.value)}
        value={lang}
      >
        {langs.map((l, i) => (
          <option value={l} key={i}>
            {l}
          </option>
        ))}
      </select>
    </Layout>
  );
};

export default IndexPage;

Store selected language (localStorage)

use-mini18n stores the selected language information in localStorage.

Screen shot of about of store selected language

Disable localStorage

It is also possible to disable the use of localStorage by passing an option during initialization.

<TransProvider i18n={i18n} enableLocalStorage={false}>
  <App />
</TransProvider>

Specify localStorage key

If you want to specify the key for localStorage, initialize it this way.

<TransProvider i18n={i18n} localStorageKey="specifyLocalStorageKey">
  <App />
</TransProvider>

Development

Test

npm run test

Code format

test run fmt

When trying out a version under development in an example app (Next.js)

You can try out the built library with the sample application under the example directory. Rewrite the code as shown below. (The built library is assumed to be stored in example/dev-lib)

diff --git a/examples/nextjs-with-typescript/pages/_app.tsx b/examples/nextjs-with-typescript/pages/_app.tsx
index d07d392..66c97ec 100644
--- a/examples/nextjs-with-typescript/pages/_app.tsx
+++ b/examples/nextjs-with-typescript/pages/_app.tsx
@@ -1,4 +1,4 @@
-import { TransProvider } from 'use-mini18n';
+import { TransProvider } from '../dev-lib/use-mini18n';
 import i18n from '../i18n';
 import type { AppProps } from 'next/app';

diff --git a/examples/nextjs-with-typescript/pages/index.tsx b/examples/nextjs-with-typescript/pages/index.tsx
index a603408..756848f 100644
--- a/examples/nextjs-with-typescript/pages/index.tsx
+++ b/examples/nextjs-with-typescript/pages/index.tsx
@@ -1,5 +1,5 @@
 import Layout from '../components/Layout';
-import { useI18n } from 'use-mini18n';
+import { useI18n } from '../dev-lib/use-mini18n';

 const IndexPage = () => {
   const { t, lang, langs, changeLang, getText } = useI18n();

Licence

MIT (except for examples/ directory)

In the example directory, the code Example created with Next.js is stored. Please refer to the Next.js page for the Licence here.

Author

Yuki Shindo