|
| 1 | + |
| 2 | +# Dictionary |
| 3 | + |
| 4 | +Dictionary is a JS library that helps automate language switching. |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +To install, use: |
| 12 | + |
| 13 | +```bash |
| 14 | + npm install dictionary |
| 15 | +``` |
| 16 | + |
| 17 | + |
| 18 | +## How to use |
| 19 | + |
| 20 | +First, we create JSON files where all data that is subject to language change will be stored. |
| 21 | +```javascript |
| 22 | +//For English lang |
| 23 | +{ |
| 24 | + "title": "Dictionary" |
| 25 | +} |
| 26 | + |
| 27 | +//And for Deutschland lang |
| 28 | +{ |
| 29 | + "title": "Wörterbuch" |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +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. |
| 34 | +```javascript |
| 35 | +// You can type any param name "param" or "num" or "sometrash". Wherever you want. |
| 36 | +{ |
| 37 | + "text": "Text for <d.param> exemple" |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +There may be several such parameters. |
| 42 | +```javascript |
| 43 | +{ |
| 44 | + "text": "Text <d.num> for <d.param> exemple <d.param>" |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Then we create an index file and create a dictionary. |
| 49 | +```javascript |
| 50 | +const initialState = { |
| 51 | + en: english, |
| 52 | + de: deutschland |
| 53 | +} |
| 54 | + |
| 55 | +export const dictionaryLangs = createDictionary(initialState) |
| 56 | +``` |
| 57 | + |
| 58 | +To access the dictionary at the top level of the application, we need to connect our dictionary. |
| 59 | +```javascript |
| 60 | +import React from 'react'; |
| 61 | +import ReactDOM from 'react-dom/client'; |
| 62 | +import App from './App'; |
| 63 | + |
| 64 | +import { Dictionary } from "./Dictionary"; |
| 65 | +import { dictionaryLangs } from "./dictionary/dictionaryLangs"; |
| 66 | + |
| 67 | +const root = ReactDOM.createRoot( |
| 68 | + document.getElementById('main') as HTMLElement |
| 69 | +); |
| 70 | + |
| 71 | +root.render( |
| 72 | + <Dictionary languages={dictionaryLangs}> |
| 73 | + <App /> |
| 74 | + </Dictionary> |
| 75 | +); |
| 76 | +``` |
| 77 | + |
| 78 | +After that, to get data from the dictionary, we call the `useDictionary()` function. |
| 79 | +```javascript |
| 80 | +// {} in the second parameter is required |
| 81 | +const title = useDictionary(state => state.title, {}), |
| 82 | +``` |
| 83 | + |
| 84 | +Here I should say that the dictionary selects the first language you added as the main one. |
| 85 | + |
| 86 | +If the text contains a parameter that needs to be replaced, we pass it as the second argument. |
| 87 | +```javascript |
| 88 | +// Here we should use only those parameters that we have noted in the text. |
| 89 | +const text = useDictionary(state => state.text, {param: "some param", num: 2}), |
| 90 | +``` |
| 91 | + |
| 92 | +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. |
| 93 | +```javascript |
| 94 | +const changeLang = useLangChanger() |
| 95 | + |
| 96 | +const handleClick = () => { |
| 97 | + changeLang("de") |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +Congratulations, you are amazing. |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +## Features |
| 106 | + |
| 107 | +- Code optimization |
| 108 | +- Possibility to obtain processed data for the entire section at once |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +## Authors |
| 113 | + |
| 114 | +- [@SrPumpkin](https://www.github.com/SrPumpkin) |
| 115 | + |
| 116 | + |
| 117 | +## License |
| 118 | + |
| 119 | +[MIT](https://choosealicense.com/licenses/mit/) |
| 120 | + |
0 commit comments