Chinese npm package 汉译英
Tags: npm, typescript, opensource, todayilearned
Published: 2023-02-24 12:35:17

## Why create a npm package ?

Just to:

- Understand what i use when i use `npm i ...` or `yarn add ...`
- Especially understand how they made it !

So, i create my first package !

## What is the library ?

The library named [**Cedict dictionary**][npmLink] and it's actually available on [npm.js](https://www.npmjs.com/) and [yarnpkg.com](https://yarnpkg.com/package/@tykok/cedict-dictionary) !

This library simply give you some functions and a large JSON file with for each object the :

- Traditional character in Chinese
- Simplified character in Chinese
- Pinyin pronunciation
- English translation

<div align="center">

![Thank you Panda](https://media.giphy.com/media/Wh6vs1icbibhm/giphy.gif)

</div>

Here is an example of what you can have in an object :

```js
{ 
  traditional: "上天入地", 
  simplified: "上天入地", 
  pinyin: "shang4", 
  english: "lit. to go up to heaven or down to Hades (idiom)/fig. to go to great lengths/to search heaven and earth/" 
}
```

> ℹ️ I do not currently speak Chinese while writing this article! 
> It’s a really complex language, though the C language and all its pointers 🤣.

### What is the Cedict ?

The Cedict, called [CC-CEDICT][cedictProjectLink] is a continuation of the CEDICT project.

This project provide a complete downloadable Chinese to English dictionary, and all of this in a specific format of CC-CEDICT.
It's an **open source** project who is actually frequently updated !

> ℹ️ If you want, you can go to her [wiki](wikiCedictLink).

### How the [@tykok/cedict-dictionary][npmLink] package work 

The project have two parts :

- The part who get data and format the Cedict into a JSON.
- The part who simply exploit the JSON.

---

To get the final JSON, we need to 

- Download the `.zip` on [CC-CEDICT][cedictProjectLink] project.
- Unzip it.
- Get the file who contain the dictionary.
- Parse it into a JSON file.

All this actions are in a script called by a cronjob who simply execute this script and update the JSON Cedict of the library.

---

The second part is more simply than the previous one !

Here, we have a JSON file. And we can simply exploit it with some functions to get for example a chinese object with a traditional character.

### A simple start with [@tykok/cedict-dictionary][npmLink]

To start, use `yarn` or `npm` to install the dependency :

```bash
# With yarn
yarn add @tykok/cedict-dictionary

# With npm
npm i @tykok/cedict-dictionary
```

Next, just import the global class to use the Cedict :

```ts
import Cedict from '@tykok/cedict-dictionary'
```

And, with the `Cedict` class, you can use some function to get a specific chinese character, the pronunciation or all the cedict !

> To know how to use this package, you can go to the [documentation][projectDocumentationLink].

## Conclusion

This project is not just for fun. I create it to learn some things i actually don't know or improve it !

<div align="center">

  ![Code boy](https://media0.giphy.com/media/bGgsc5mWoryfgKBx1u/giphy.gif?cid=790b761115bee9bc49639ad6455d739de8e5ea0f6a333594&rid=giphy.gif&ct=g)

</div>

Of course, this project is **open source** and you can come to contribute to this project. There are still many things to improve !


---

Tell me if you have any suggestions, or remarks for the project. And of course if this article was interesting.

<div align="center">
<a href="https://www.buymeacoffee.com/tykok"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a sushi&emoji=🍣&slug=tykok&button_colour=5F7FFF&font_colour=ffffff&font_family=Cookie&outline_colour=000000&coffee_colour=FFDD00" /></a>
</div>

[githuhLink]: https://github.com/Tykok/cedict-chinese-transformation
[npmLink]: https://www.npmjs.com/package/@tykok/cedict-dictionary
[projectDocumentationLink]: https://tykok.github.io/cedict-chinese-transformation/
[cedictProjectLink]: https://www.mdbg.net/chinese/dictionary?page=cedict
[wikiCedictLink]: https://cc-cedict.org/wiki/