Skip to content

Commit 24a1c5f

Browse files
committed
docs: insertValues more clear
1 parent 6268194 commit 24a1c5f

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -2247,19 +2247,23 @@ Pluralization is not supported but can be achieved manually by adding variations
22472247
### tr()
22482248
Usage:
22492249
```ts
2250-
tr(key: string, ...values: Stringifiable[]): string
2250+
tr(key: string, ...insertValues: Stringifiable[]): string
22512251
```
22522252

22532253
The function returns the translation of the passed key in the language added by [`tr.addLanguage()`](#traddlanguage) and set by [`tr.setLanguage()`](#trsetlanguage)
2254-
Should the translation contain placeholders in the format `%n`, where `n` is the number of the value starting at 1, they will be replaced with the respective item of the `values` rest parameter.
2255-
The items of the `values` rest parameter will be stringified using `toString()` (see [Stringifiable](#stringifiable)) before being inserted into the translation.
2254+
Should the translation contain placeholders in the format `%n`, where `n` is the number of the value starting at 1, they will be replaced with the respective item of the `insertValues` rest parameter.
2255+
The items of the `insertValues` rest parameter will be stringified using `toString()` (see [Stringifiable](#stringifiable)) before being inserted into the translation.
22562256

22572257
Should you be using nested objects in your translations, you can use the dot notation to access them.
22582258
First, the key will be split by dots and the parts will be used to traverse the translation object.
22592259
If that doesn't yield a result, the function will try to access the key including dots on the top level of the translation object.
22602260
If that also doesn't yield a result, the key itself will be returned.
22612261

2262-
If no language has been added or set before calling this function, it will return the key itself.
2262+
If no language has been added or set before calling this function, it will also return the key itself.
2263+
2264+
To check if a translation has been found, compare the returned value with the key. If they are the same, the translation was not found.
2265+
You could also write a wrapper function that can then return a default value or `null` if the translation was not found instead.
2266+
22632267
If the key is found and the translation contains placeholders but none or an insufficient amount of values are passed, it will try to insert as many values as were passed and leave the rest of the placeholders untouched in their `%n` format.
22642268
If the key is found, the translation doesn't contain placeholders but values are still passed, the values will be ignored and the translation will be returned without modification.
22652269

@@ -2308,7 +2312,7 @@ console.log(tr.forLang("en", "welcome.generic")); // "Welcome"
23082312
### tr.forLang()
23092313
Usage:
23102314
```ts
2311-
tr.forLang(language: string, key: string, ...values: Stringifiable[]): string
2315+
tr.forLang(language: string, key: string, ...insertValues: Stringifiable[]): string
23122316
```
23132317

23142318
Returns the translation of the passed key in the specified language. Otherwise behaves exactly like [`tr()`](#tr)

0 commit comments

Comments
 (0)