Skip to content

Commit a1cec79

Browse files
committed
Dictionary init
0 parents  commit a1cec79

File tree

14 files changed

+5773
-0
lines changed

14 files changed

+5773
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015-present Dan Abramov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)