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

@zyda/next-localization

v0.4.0

Published

Localization for Next.js

Downloads

4

Readme

Next Localization

This package exposes a higher order component that wraps Next's app or pages, to provide translation function to them along side with the current app language.

This package is optimized to work with serverless next build (on vercel or netlify or lambda function or whatever platform). Other alternatives such as i18n would be more optimized for usage with server-based next application, but we found they do not work properly with serverless build.

Installation

Install with npm...

npm i @zyda/next-localization

Or with yarn...

yarn add @zyda/next-localization

API

This package withNextLocalization (explained below) higher order component as a default export. It also exports useLocalization hook as a named export.

withNextLocalization

As stated above, the localization is done through a Higher Order Component (HOC) that wraps the entire Next page and provides it with a translation function as a prop. That HOC accepts 5 parameters and they are as follows:

  1. settings object.
  2. buildDictionary Function.
  3. getLanguage Function.
  4. usedSources Array of strings.
  5. page: Next.js page that needs to be localized.

Explaining these parameters makes more sense if they are explained from the last to the first: Page: This is just the page that needs to be localized. The result of the localization HOC is another Next.js page that wraps this page, and does no changes to its rendering, only provides 2 props to the original page: t function that is used for translation, and lang string that indicates the used language.

usedSources: Each page may require a different set of strings to be used in the localization of that page, theses sources are listed in this array, and are passed to buildDictionary function that is described later on. This parameter is used to limit the amount of fetched resources and also to reduce the resulting dictionary so that each page does not contain a lot of useless strings.

getLanguage: This is a function that accepts PageContext or AppContext as localization can be used to wrap the whole Next app, and also accepts an object that contains all props that were initially returned by the page's getInitialProps. This function is expected to return a string value that is treated as the language (no constraints on the string), that value is later passed to the buildDictionary function and to the wrapped page. So while there are no constraints on the string that can be returned, the rest of the app should be able to handle all the possible returned values. This function is how you tell the app what language it should use to localize the page.

buildDictionary: This is an async function that accepts the result of the getLanguage function and the value of usedSources in the mentioned order. This function is responsible for providing the dictionary that is used to translate the wrapped page. In case the page required some keys that cannot be found in the dictionary an exception will be thrown.

settings: This is the main settings for the localization. For now, it only contains 1 key: customTranslation (more on that later).

The HOC function is curried. And it's intended to be used as follows: A specialized version of the HOC should be created and that specialized version should be used with the pages. An example for that specialized version is to use it to always provide some values for the HOC like settings, buildDictionary, and getLanguage.

So the resulting code would be something like the following:

// The specialized version
const withLocalization = withNextLocalization(
  {
    customTranslations: [ /* custom translations ... */ ]
  },
  myDictionaryBuilder,
  getLocalizationLanguage
);

// Its usage in some app page...
const MyPage = ({ t: Translate, lang: string, /* some other props from Next */ }) => {
  /* Page logic ... */
};

export withLocalization(MyPage);

useLocalization

This is custom hook that returns an object of { t: (string) => string, lang: string } which are the same props that get passed to the localized page.

The hook accepts one optional argument:

  • customTranslation: The same object that is passed to the localization HOC, defaults to empty array.