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

next-translator

v3.0.4

Published

Simple multilanguage for nextjs

Downloads

14

Readme

next-translator

next-translator is a simple yet powerful translation library for Next.js applications, offering seamless integration for both server-side and client-side translation management.

Features

  • Easy to configure and use.
  • Supports both server-side and client-side rendering.
  • Dynamic translation loading based on user preferences or defaults.
  • Support for variables in translation strings.

Installation

To install next-translator, run the following command:

npm install next-translator

Setup

Configuring Translations

First, create a configuration file for managing your translations. You can simply copy and paste the following example. Make sure to keep the directory structure unchanged for seamless integration.

// utils/initializeTranslations.ts
import { cookies } from 'next/headers';
import { ServerConfig, configTR } from 'next-translator';

const initializeTranslations = async () => {
    const config: configTR = {
        defaultLang: 'it',
        langs: ['it']
    };

    const nextCookies = cookies();
    let language = nextCookies.get('lang')?.value || config.defaultLang;
    let translations;
    try {
        translations = (await import(`@/locales/${language}.json`)).default;
    } catch (e) {
        throw new Error('Language not found');
    }
    const data: ServerConfig = {
        translations,
        config
    };
    return { data };
};

export default initializeTranslations;

Creating the Locales Directory

Create a locales directory in your project to store your JSON translation files (e.g., it.json, en.json).

Usage

Using Variables in Translations

You can include variables in your JSON translation files like this:

{
    "hello": "Hello %s"
}

And use them in your translations:

t('hello', 'zxcvbinz');

This feature is available both on the server and client side.

Server-Side Rendering

To use next-translator in a server-side rendered page, import and initialize translations as follows:

// pages/home.tsx
import initializeTranslations from '@/utils/translations';
import { CreateServerProvider } from 'next-translator';

export default async function Home() {
    const { data } = await initializeTranslations();
    const { t } = CreateServerProvider(data).useServerTranslator('server');

    return (
        <>
            <h1>{t('name')}</h1>
        </>
    );
}

Client-Side Rendering

For client-side rendering, set up the TranslationProvider in your layout.tsx:

// layout.tsx
import type { Metadata } from 'next';
import { Inter } from 'next/font/google';
import './globals.css';
import initializeTranslations from '@/utils/translations';
import { TranslationProvider } from 'next-translator';

const inter = Inter({ subsets: ['latin'] });

export const metadata: Metadata = {
    title: 'Create Next App',
    description: 'Generated by create next app'
};

export default async function RootLayout({ children }: { children: React.ReactNode }) {
    const { data } = await initializeTranslations();

    return (
        <html lang="it">
            <body className={inter.className}>
                <TranslationProvider data={data}>{children}</TranslationProvider>
            </body>
        </html>
    );
}

Then, use the translator in your app:

// pages/home.tsx
'use client';
import { useTranslator } from 'next-translator';

export default async function Home() {
    const { t } = useTranslator('client');

    return (
        <>
            <h1>{t('name')}</h1>
        </>
    );
}

Building the Package

To build the package, run:

npm run build # Creates dist folder
npm pack # Creates a tar.gz file