Skip to content

Commit 426762d

Browse files
authored
Merge pull request #7 from hodfords-solutions/chore/update-readme
chore: update readme
2 parents cab583b + 38c92cd commit 426762d

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

README.md

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
We are using package https://www.npmjs.com/package/nestjs-i18n. So check out the info here.
1+
<p align="center">
2+
<a href="http://opensource.hodfords.uk" target="blank"><img src="https://opensource.hodfords.uk/img/logo.svg" width="320" alt="Hodfords Logo" /></a>
3+
</p>
24

3-
# Module
5+
<p align="center"> <b>nestjs-cls-translation</b> provides context-aware translations in NestJS applications using <b>Context-Local Storage</b> (CLS), making it easier to manage and access locale-specific data across different parts of your application.</p>
6+
7+
## Installation 🤖
8+
9+
Install the `nestjs-cls-translation` package with:
10+
11+
```bash
12+
npm install @hodfords/nestjs-cls-translation --save
413
```
14+
15+
You'll need to configure the translation module by adding it to your NestJS app's module setup. Here’s how you can configure it:
16+
17+
```typescript
518
TranslationModule.forRoot({
619
fallbackLanguage: 'en',
720
parser: I18nJsonParser,
@@ -11,25 +24,44 @@ TranslationModule.forRoot({
1124
},
1225
resolvers: [new HeaderResolver(['language'])]
1326
});
14-
1527
```
1628

17-
# Translate
18-
```
19-
await trans('error.an_error_occurred')
20-
```
29+
## Usage 🚀
2130

22-
# Get current language
23-
```
24-
currentLanguage()
25-
```
31+
#### Translation Functions
32+
33+
To translate a specific key, use the trans function, passing the key for the translation string you wish to fetch:
2634

27-
# Get default language
35+
```typescript
36+
const translatedText = trans('error.an_error_occurred')
2837
```
29-
defaultLanguage()
38+
39+
This will return the translated string based on the user's current language, or the fallback language if no specific translation exists for the user's language.
40+
41+
#### Get Current Language
42+
43+
To retrieve the language currently being used in the context of a request, use the `currentLanguage()` function:
44+
45+
```typescript
46+
const currentLang = currentLanguage()
3047
```
3148

32-
# Run with a language
49+
#### Get Default Language
50+
51+
If you need to access the application's default or fallback language (set in the module configuration), use the `defaultLanguage()` function:
52+
53+
```typescript
54+
const defaultLang = defaultLanguage()
3355
```
34-
await runInLanguage('en', () => ...);
56+
57+
#### Run with a Specific Language Context
58+
59+
You may want to execute certain parts of your code in a specific language context. The runInLanguage() function allows you to run a block of code under a designated language context, overriding the current language:
60+
61+
```typescript
62+
await runInLanguage('en', () => {...});
3563
```
64+
65+
## License 📝
66+
67+
This project is licensed under the MIT License

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hodfords/nestjs-cls-translation",
3-
"version": "10.1.0",
3+
"version": "10.1.1",
44
"description": "Provides context-aware translations in NestJS using CLS",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)