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

dictionary-react

v1.2.2

Published

Dictionary is a JS library that helps automate language switching. This library can greatly speed up the development of language versions for any type of application. This library is new and requires a lot of improvements, but you can already try it out.

Downloads

1,730

Readme

Dictionary

Dictionary is a JS library that helps automate language switching.

This library can greatly speed up the development of language versions for any type of application. This library is new and requires a lot of improvements, but you can already try it out. Initially, it was supposed to use Dictionary with React.

Installation

To install, use:

  npm install dictionary

Documentation

  • createDictionary
  • useDictionary
  • useLangChanger

createDictionary(languages: object, lang?: string)

Used to create a dictionary object where all languages will be stored.

languages – here we place an object with languages

lang – if necessary, we specify the default language from those available in the object. If the parameter is not specified, the library takes the first specified language from languages ​​as the default language.

Exemple

import { createDictionary } from "dictionary-react";
import english from "./english.json"
import deutschland from "./deutschland.json"

const initialState = {
    en: english,
    de: deutschland
}

export const dictionaryLangs = createDictionary(initialState)

useDictionary(selector: (state: any) => any, dep?: object | undefined)

Used to get data from a dictionary. It is designed so that it will give out data of the language that is active.

selector – we specify a function to get the section of the dictionary that is needed

dep – an object with one or more variables specified in the received string is passed.

Note: For now, it only works when getting one string from the dictionary.

Exemple

import {useDictionary} from "dictionary-react";

const page = useDictionary(state =>  state.page)
// Returns an object containing information for page

// If the dictionary contains {page: {title: "Some title <d.exemple>"}}
page.title = useDictionary(state =>  state.page.title, {exemple: "testing"})
// Returns “Some title testing” 

useLangChanger(lang: string)

Used when it is necessary to switch the language in the application.

lang – we pass a string with the name of the language available in the dictionary

Exemple

const changeLang = useLangChanger()
const handleClick = changeLang("de")

How to use

First, we create JSON files where all data that is subject to language change will be stored.

//For English lang
{
    "title": "Dictionary"
}

//And for Deutschland lang
{
    "title": "Wörterbuch"
}

If the text should contain some dynamic parameter, add <d.*someVariable*> to this place, where someVariable is an identifier that is used to replace this section with the parameter being passed.

// You can type any param name "param" or "num" or "sometrash". Wherever you want.
{
    "text": "Text for <d.param> exemple"
}

There may be several such parameters.

{
    "text": "Text <d.num> for <d.param> exemple <d.param>"
}

Then we create an index file and create a dictionary.

const initialState = {
    en: english,
    de: deutschland
}

export const dictionaryLangs = createDictionary(initialState)

To access the dictionary at the top level of the application, we need to connect our dictionary.

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

import { Dictionary } from "./Dictionary";
import { dictionaryLangs } from "./dictionary/dictionaryLangs";

const root = ReactDOM.createRoot(
    document.getElementById('main') as HTMLElement
);

root.render(
    <Dictionary languages={dictionaryLangs}>
        <App />
    </Dictionary>
);

After that, to get data from the dictionary, we call the useDictionary() function.

const title = useDictionary(state => state.title),

Here I should say that the dictionary selects the first language you added as the main one.

If the text contains a parameter that needs to be replaced, we pass it as the second argument.

// Here we should use only those parameters that we have noted in the text.
const text = useDictionary(state => state.text, {param: "some param", num: 2}),

To change the language, you need to initialize the useLangChanger() function and then pass the name of the language you added earlier as an argument.

const changeLang = useLangChanger()
const handleClick = () => {
    changeLang("de")
}

Congratulations, you are amazing.

Features

  • Code optimization
  • AI translation

Authors

License

MIT