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

gen-typescript-locale

v0.1.8

Published

This is a little library for the translation of your phrases This library is the fastest one in whole libraries I have seen

Downloads

2

Readme

What is it?

This is a little library for the translation of your phrases This library is the fastest one in whole libraries I have seen

Required file structure

Required structure of locale folder, this folder may located anywhere you want

src
    locale
        translates
            english.ts                // here phrases translation should be located
            ... (other languages)
        generatedTypes.ts             // this file will be generated
        index.ts                      // this is a library file
        phrases.txt                   // here application phrases will be located

Quick start

npm install --save-dev gen-typescript-locale

1) add next code to the file: ./src/locale/index.ts

import {IPhraseType} from './generatedTypes';
const language = localStorage.getItem('language');

let dict = {};

if (language) {
   dict = require(`./translates/${language}`).dict;
} else {
   dict = require('./translates/english').dict;
}

type LanguageType = 'english'|'russian';
export const setLanguage = (language: LanguageType) => {
   localStorage.setItem('language', language);
   window.location.reload();
};

export const _ = (phrase: IPhraseType, replacers: Object = {}): string => {
   let foundPhrase: string = dict[phrase];

   Object.keys(replacers).forEach(key => {
      foundPhrase = foundPhrase.replace(new RegExp(`{${key}}`, 'g'), replacers[key]);
   });

   return foundPhrase;
};

This is a library file, here defined logic how to manage phrases from the language file. If you need custom logic you can change it of course

2) add next content to the file: ./src/locale/phrases.txt

// common
January

// this is some unique page
Hello {name}

The only place where you want to define phrases which your application will using. As you can see the syntax is quite simple:

  • // - comment
  • January - phrase in you app, by this phrase will be generated TS code to make your life easy
  • Hello {name} - phrase in you app with variable, in TS code you can pass the value in this way: _('Hello {name}', {name: 'Your name'})

3) add translation of ./src/locale/translates/english.ts

import {IDictionaryType} from '../generatedTypes';

export const dict: IDictionaryType = {
   // gen-typescript-locale start point
   'January': 'January',
   'Hello {name}': 'Hello {name}'
   // gen-typescript-locale end point
};

ignore that IDictionaryType not found, it will be generated after next command.

Comments // gen-typescript-locale start/end point are required!

4) add script for code generation to the ./package.json

{
   "scripts": {
      "gen_locale": "node ./node_modules/gen-typescript-locale --dist='./src/locale'"
   }
}

Parameter --dist should points to the folder locale with all files written in prev steps

5) generate code

npm run gen_locale

6) Add usage somewhere in your application

import {_, setLanguage} from './src/locale'

_('January'); // returns 'January'
_('Hello {name}', {name: 'Yuriy'}); // returns 'Hello Yuriy'

setLanguage('russian');
setLanguage('english');