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-translate-localekeys

v1.0.0

Published

generates typesafe locale keys for next-translate package with auto completion and vsc comments support

Downloads

5

Readme

Next-translate LocaleKeys

Intended for use in conjunction with next-translate. Used for generating all possible keys for the useTranslation hook with type safety.

How to install

npm install --save-dev next-translate-localekeys

How to run

npx next-translate-localekeys

Example one (Mode: "generate"):

Requirements

  1. Translation Files are in /examples/locales/en
  2. Goal to get all my locale keys in the generated (/examples/generated) folder with typescript support and translation comments
  3. Using default seperator for next-translate

Actions

  1. run npx next-translate-localeKeys generate --rootDir ./examples/locales --outDir ./examples/generated --typescript --translations
  2. Go to your file where you want to use the useTranslation hook
import { FC } from 'react'; 
import useTranslation from 'next-translate/useTranslation';
import { LocaleKeys } from './examples/generated/locale_keys.g';

export const DivWithTitleFromCommonNamespace: FC = () => {
    const { t } = useTranslation(LocaleKeys.common);

    return <div>{t(LocaleKeys._common.title)}</div>;
}

Example two (Mode: "compile"):

Requirements

  1. All the source code of the project which is using the locale keys object is located in examples/compiled/after
  2. typescript project
  3. only in production

Actions

  1. Optional: insert command into pipeline (github action)
  2. run npx next-translate-localekeys compile --rootDir ./examples/compiled/after --typescript
  3. see result:

before:

import useTranslation from "next-translate/useTranslation";

import { LocaleKeys } from "../../generated/locale_keys.g";

export const Test = () => {
  const { t } = useTranslation(LocaleKeys.common);

  return (
    <div>
      <Test2 
        home={LocaleKeys._home + ''}
        localeKey2={LocaleKeys._dynamic.example_of_dynamic_translation       }
       />
      <div>{t(LocaleKeys._common.title)}</div>
      <div>{t(LocaleKeys._home.description )}</div>
      <div>{t(`${LocaleKeys._more_examples.   _nested_example.very_nested  }.nested`)}</div>
    </div>
  );
};

const Test2 = ({ home, localeKey2 }) => {
  const { t } = useTranslation(home);
  const localeKey = home + '.description';
    
  return <div>{t(localeKey)}{t(localeKey2)}</div>
};

after:

import useTranslation from "next-translate/useTranslation";


export const Test = () => {
  const { t } = useTranslation("common");

  return (
    <div>
      <Test2 
        home={"home"+ ''} 
        localeKey2="dynamic:example-of-dynamic-translation"
       />
      <div>{t("common:title")}</div>
      <div>{t("home:description")}</div>
      <div>{t(`more_examples:nested-example.very-nested.nested`)}</div>
    </div>
  );
};

const Test2 = ({ home, localeKey2 }) => {
  const { t } = useTranslation(home);
  const localeKey = home + '.description';
    
  return <div>{t(localeKey)}{t(localeKey2)}</div>
};

Configuration

To get all configurations possible: npx next-translate-localekeys --help

| Flag | Description | Type | Default | Required | | ---- | ----------- | ---- | ------- | -------- | | mode | current mode | "compile" or "generate" | - | * | rootDir | location of the source code. | string | - | * | outDir | place of the generated output. | string | rootDir | | errDir | location of error file. | string | rootDir | | typescript | enables typescript. | boolean | false | | translations | enables translation comments. | boolean | false | | lang | language that should be used in translation comments | string | en | | nsSeparator | char to split namespace from key. | string | ":" | | keySeparator | change the separator that is used for nested keys. | string | "." |