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

i18n-text-replacer

v1.0.0

Published

Replace the language in the front-end source code with one click and turn it into an i18n system

Downloads

6

Readme

I18n-Replacer

Introduction

I18n replacer is an internationalization tool that automatically replaces language text in the source code(typescript) and generates multi-language files through syntax analysis of ts source code(based on typescript compiler).

I hope that websites in different languages ​​can benefit more people in the world, hope world peace.

features:

  • Write the code as if it were not an international project. Then run a one-line command to replace all locale text with i18n keys and generate a message file.
  • The languages ​​to be replaced and the languages ​​to be generated can be configured.
  • Automatically merge template strings(jsx text) with variables.
  • Integrated with react-intl library. (you could write your own custom render class by implement I18nFormatter and pass your class as params.)
  • Supports hook mode (changing language in real time) and global mode, and can use custom render classes to determine how to replace source code and generate language files.
  • Test coverage 98%.

Example

Your code Before:

import React from 'react';

function Component() {
    const en = 'English';
    const cn = 'Chinese';
    const locales = `${en} and ${cn}`;

    return (
        <div>
            Please choose your locale from: {en} {cn}
        </div>
    );
}

Your code will be replaced with the below after run command:

import { useIntl, FormattedMessage } from 'react-intl';
import React from 'react';

function Component() {
    const intl = useIntl();

    const en = intl.formatMessage({
        id: 'key0001',
        defaultMessage: 'English',
    });
    const cn = intl.formatMessage({
        id: 'key0002',
        defaultMessage: 'Chinese',
    });
    const locales = intl.formatMessage({
        id: 'key0003',
        defaultMessage: '{v1} and {v2}',
        values: { v1: en, v2: cn },
    });

    return (
        <div>
            <FormattedMessage
                id="key0004"
                defaultMessage="Please choose your locale from: {v1} {v2}"
                values={{ v1: en, v2: cn }}
            />
        </div>
    );
}

You will get some new files including some locale message files and a provider which you could import and put it in your root file.

/*
* This file will be changed by automatic program.
* You can only change variable's property and value.
*/
import { LocalKey } from './types';

const locale: Record<LocalKey, string> = {
    key0001: 'English',
    key0002: 'Chinese',
    key0003: '{v1} and {v2}',
    key0004: 'Please choose your locale from: {v1} {v2}',
};

export default locale;

Quick start

  1. cli

    npx i18n you can get specific info with npx i18n -h

  2. api

write your own node script like this and run it

import I18nReplacer, { GlobalI18nFormatter } from 'i18-replacer';
import { ScriptTarget } from 'typescript';

I18nReplacer.createI18nReplacer().replace();

or with commonJs to run it:

const { HookI18nFormatter } = require("i18-replacer")

const I18nReplacer = require("i18-replacer").default;

I18nReplacer.createI18nReplacer().replace()

the options is like this:

export interface ReplacerOpt {
    // file or dirs to extract messages default is [process.cwd()]
    targets?: string[];
    // The location where the locale message files are generated default is [process.cwd()/i18n]
    distLocaleDir?: string;
    // the locale which is searched in targets default is zh-cn
    localeToReplace?: LocaleTypes;
    // the locale file default is [en-us, zh-cn]
    localesToGenerate?: LocaleTypes[];
    // formatter class, use could provide class implement the `I18nFormatter` class,
    // default provide two class:GlobalI18nFormatter and HookI18nFormatter
    I18nFormatterClass?: I18nFormatterCtr;
    // default is hook
    I18nFormatterClassAlias?: 'hook' | 'global';
    // if rename the key to meaningful variable if has related english translate
    meaningKey?: boolean;
    // decide which file or dir could be handled
    filters?: Filter[];
    // file name or dir to ignore
    excludes?: string[];
    debug?: boolean;
}

License

The MIT License.