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

teyfik-i18n-next

v0.0.27

Published

Seamless i18n integration for Next.js. Manage locales, route localization, dynamic content translation, and more. Reach a global audience effortlessly. #Nextjs #i18n

Downloads

11

Readme

teyfik-i18n-next

Seamless i18n integration for Next.js. Manage locales, route localization, dynamic content translation, and more. Reach a global audience effortlessly. #Nextjs #i18n

Usage

Installation

Install teyfik-i18n-next

npm i teyfik-i18n-next

Install teyfik-directory-loader if you don't have a custom loader

npm i teyfik-directory-loader

Next.js configuration

Configure your i18n settings in next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
  i18n: {
    defaultLocale: 'en',
    locales: ['en', 'de'],
  },
};

module.exports = nextConfig;

Initializing pageWithTranslations helper

teyfik-i18n-next uses a function called pageWithTranslationsFactory to create a helper function to use in every page of the app. The factory function takes a single argument called loader, and the loader function takes a single argument called locale then returns all the translations for given locale.

  1. Using teyfik-directory-loader

With the help of teyfik-directory-loader, you can load every JSON and YAML files in a specific directory keeping folder structure. Read more about the package here

import directoryLoader from 'teyfik-directory-loader';
import { pageWithTranslations } from 'teyfik-i18n-next';

const pageWithTranslations = pageWithTranslationsFactory((locale) =>
  directoryLoader('public', 'locales', locale),
);

export default pageWithTranslations;
  1. Using a custom loader
import { pageWithTranslations } from 'teyfik-i18n-next';

const pageWithTranslations = pageWithTranslationsFactory((locale) =>
  fetch(`https://example.com/locales/${locale}.json`),
);

export default pageWithTranslations;

The loader's return value should return all the translations for the given locale, as shown in the example below:

{
  "common": {
    "title": "Hello world!",
    "description": "Lorem ipsum dolor sit amet consectetur adipisicing elit..."
  },
  "index": {
    "title": "Home"
  },
  "footer": {
    "copyright": "Copyright © 2023"
  }
}

If you prefer to use teyfik-directory-loader, your directory structure should be similar to the following:

.
└── path
  └── to
    └── locales
      ├── en
      | └── common.json
      └── de
        └── common.json

Using the pageWithTranslations helper

After you initialize a loader and defined pageWithTranslations helper you should use it in your pages to load translations:

import { useTranslation } from 'teyfik-i18n-next';
import { pageWithTranslations } from '../helpers/pageWithTranslations';

export default function Home() {
  const { t } = useTranslation();

  return (
    <section>
      <header>
        <h1>{t('common.title')}</h1>
      </header>
    </section>
  );
}

export const getStaticProps = pageWithTranslations('common', (context) => {
  // Your custom getStaticProps logic here

  return {
    props: {},
  };
});

You can define filtered namespaces on property level such as:

export const getStaticProps = pageWithTranslations('common.title');

Finally, wrap your app with I18nProvider. In the example below, i18n prop comes from pageWithTranslations helper that we used with getStaticProps.

import type { AppProps } from 'next/app';
import { I18nProvider } from 'teyfik-i18n-next';

function App({ Component, pageProps }: AppProps) {
  return (
    <I18nProvider i18n={pageProps.i18n}>
      <Component {...pageProps} />
    </I18nProvider>
  );
}

export default App;

That's it!

Plurals

In the example below, the translation of profile.viewCount that is tried to be accessed will be changed to profile.viewCount.other. Other possible values will be profile.viewCount.zero and profile.viewCount.one.

export default function Page() {
  const { t } = useTranslation();

  return (
    <section>
      <main>
        <p>{t('profile.viewCount', { count: 5 })}</p>
      </main>
    </section>
  );
}

Answers

In the example below, the translation of profile.public that is tried to be accessed will be changed to profile.public.yes. Other possible values will be profile.public.no.

export default function Page() {
  const { t } = useTranslation();

  return (
    <section>
      <main>
        <p>{t('profile.public', { answer: true })}</p>
      </main>
    </section>
  );
}

Template literals

Templates will be rendered with Mustache. In the example below, {{ username }} template literal will be changed with the username that is provided in the context.

public/locales/en/profile.json

{
  "salute": "Hello, {{ username }}!"
}
export default function Profile() {
  const { t } = useTranslation();

  return (
    <section>
      <main>
        <p>{t('profile.salute', { username: 'teyfik' })}</p>
      </main>
    </section>
  );
}

The result will be "Hello, teyfik!"