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

@postlight/generate-i18n-types

v1.0.7

Published

Generate il8n string types

Downloads

13

Readme

generate-i18n-types

Postlight's Generate i18n Types generates types for internationalization strings.

Installation

Install generate-18n-types in your devDependencies

$ yarn add @postlight/generate-18n-types --dev

The package also requires installation of i18next and js-yaml as dependencies, along with @types/i18next and @types/js-yaml to avoid any Typescript errors

$ yarn add i18next js-yaml @types/i18next @types/js-yaml --save

Usage

This generator requires 3 arguments to generate your i18n types

$ generate-i18n-types --translations=path/to/translation.yml --stringTypesPath=path/to/strings.ts --utilPath=path/to/i18n.ts

--translations This is the path to the YAML file you want to generate types for. Currently this only supports input of a single file. For an example of the format of the YAML file, see example.yaml

--stringTypesPath This is the path to the file where you want the generated string types to reside.

--utilPath This is the path to the file where you want the generated translation functions for the string types to reside.

Note: You may require additional formatting of the generated utilPath file. Using Prettier as an example, you can just add the following additional command to the above && prettier --write ./path/to/i18n.ts

Once the command succeeds, it will automatically generate the enum types for the strings in the translation file, along with matching function signatures if the strings have dynamic fields. Then you can use the new translations in your code. Enjoy!

Example

generate-i18n-types takes a YAML file you want translated

translation:
  appName: 'My App'
  supportEmail: '[email protected]'
  authFirst:
    text: 'Before we can create your account, please <{{url}}|authenticate with My App>.'
  errors:
    invalidUsers: 'The following people could not be invited: {{invalidUsers}}'

Then generates the enum types for the stringTypesPath file

/* tslint:disable */
/* eslint-disable */
// This file was automatically generated and should not be edited.

export enum I18NStringsWithArgs {
  AuthFirstText = 'authFirst.text',
  ErrorsInvalidUsers = 'errors.invalidUsers',
}

export enum I18NStrings {
  AppName = 'appName',
  SupportEmail = 'supportEmail',
}

And the corresponding translate functions and required packages and imports/exports for the utilPath file

/* tslint:disable */
/* eslint-disable */
// This file was automatically generated and should not be edited.

import i18n from 'i18next';
import yaml from 'js-yaml';
import fs from 'fs';
import { I18NStrings, I18NStringsWithArgs } from '../types/strings';

export { I18NStrings, I18NStringsWithArgs } from '../types/strings';

let boundT: typeof i18n.t;

const en = yaml.safeLoad(
  fs.readFileSync('../../locales/en/translation.yml', 'utf8')
);

// ==== START OVERLOADED SIGNATURES
function translate(
  key: I18NStringsWithArgs.AuthFirstText,
  { url }: { url: string }
): string;
function translate(
  key: I18NStringsWithArgs.ErrorsInvalidUsers,
  { invalidUsers }: { invalidUsers: string }
): string;
function translate(key: I18NStrings): string;
// ==== END OVERLOADED SIGNATURES

function translate(key: string, data?: { [key: string]: string }) {
  if (boundT) {
    return boundT(key, data);
  }
  i18n.init({
    lng: 'en',
    resources: { en },
    interpolation: {
      escapeValue: false,
    },
  });
  const { t } = i18n;
  boundT = t.bind(i18n);
  return boundT(key, data);
}

export default translate;

License

Licensed under either of the below, at your preference:

  • Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

Contributing

For details on how to contribute, see CONTRIBUTING.md

Unless it is explicitly stated otherwise, any contribution intentionally submitted for inclusion in the work, as defined in the Apache-2.0 license, shall be dual licensed as above without any additional terms or conditions.


🔬 A Labs project from your friends at Postlight. Happy coding!