Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Raiondesu committed Dec 18, 2023
1 parent 4a281ff commit 66ce273
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 30 deletions.
4 changes: 2 additions & 2 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ A tiny library (3kb, zero-dependency) that allows to localize and format strings

- [Define a translation document factory](#define-a-translation-document-factory)
- [Create a translator function (`t()`)](#create-a-translator-function-t)
- [Use a translator function](#use-a-translator-function)
- [Use the translator function](#use-a-translator-function)
- [Add default plugins and processors](#add-default-plugins-and-processors)

## Usage<!-- omit from toc -->
Expand Down Expand Up @@ -52,7 +52,7 @@ const t = createTranslator(getDocument, [
]);
```

### Use a translator function
### Use the translator function

```js
console.log(t('hello')); // `Hello, World!`
Expand Down
79 changes: 69 additions & 10 deletions packages/plugins/arrays/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,35 @@ const getDocument = () => ({
}
},

gift: [
"Buy this birthday gift for"
'for price': [
"for",
{ "price": [0] }, // references the `price` key with fallback
],

'until birthday': [
"until",
{ "birthday": [] } // references the `birthday` key
],

gift: [
"Buy this birthday gift",
"for price",
"until birthday"
],

gifts: {
'intl/plural': {
one: 'gift',
other: 'gifts',
many: 'gifts',
}
},

'gifts amount': [
// Reference to the 0-th argument of `gifts`
'0:gifts',
// Reference to the `gifts` key
{ 'gifts': 0 }
]
});
```
Expand All @@ -60,15 +84,50 @@ const t = createTranslator(getDocument, [
]);
```

### Use a translator function
### Use the translator function

```ts
console.log(t('gift', { price: [123], birthday: [new Date(2023, 7, 9)] }));
// Buy this birthday gift for $123 until Aug 9, 2023
t('for price', {
// Pass parameters for the key reference
price: [123]
});
// for $123

t('gifts amount', {
// Pass parameters for the key reference
gifts: [41]
});
// 41 gift

t('gifts amount', {
// Pass parameters for the key reference
gifts: [42]
});
// 42 gifts

// Optional processor config override
console.log(t('gift', { price: [123, { currency: 'EUR' }], birthday: [new Date(2023, 7, 9)] }));
// Buy this birthday gift for €123 until Aug 9, 2023
t('for price', { price: [123, { currency: 'EUR' }] });
// for €123

// Custom separator
t('for price', { price: [123] }, ' - ');
// for - €123

// Deep key cross-reference
t('gift', {
'for price': [{ price: [123] }],
'until birthday': [{ birthday: [new Date(2023, 7, 9)] }]
})
// Buy this birthday gift for €123 until - Aug 9, 2023

// Custom separator strategy
t('gift', {
'for price': [{ price: [123] }, ' just '],
'until birthday': [new Date(2023, 7, 9), ' - ']
}, (lines) => lines.join('\n'));
// Buy this birthday gift
// for just €123
// until - Aug 9, 2023


// Parameter auto-complete and type-checking!
Expand All @@ -77,12 +136,12 @@ console.log(t('gift', { price: [123, { currency: 'EUR' }], birthday: [new Date(2
t('gift', new Date());

// TS Error: Argument of type {} is not assignable to parameter of type {...}.
// Missing properties: 'birthday', 'gift'
// Missing properties: 'until birthday', 'for price'
t('gift', { });

// TS Error: Argument of type Date is not assignable to parameter of type number.
t('gift', { price: [new Date(), { currency: 'EUR' }], birthday: [new Date(2023, 7, 9)] });
t('for price', { price: [new Date(), { currency: 'EUR' }] });

// TS Error: Expected 2 arguments, but got 1.
// TS Error: Expected 2-3 arguments, but got 1.
t('gift');
```
16 changes: 8 additions & 8 deletions packages/plugins/arrays/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ declare module 'intl-schematic/plugins' {
: unknown;
}
: unknown,
delimiter?: string | ((translatedArray: string[], defaultDelimiter: string) => string),
separator?: string | ((translatedArray: string[], defaultSeparator: string) => string),
];

// Extracts referenced keys from arrays and detects their processors and signatures
Expand Down Expand Up @@ -74,14 +74,14 @@ function match(value: unknown): value is Array<string | Record<string, unknown>>
* ```
*
* Will find all translation keys referenced, resolve them
* and join all elements using a custom delimiter (space by-default).
* and join all elements using a custom separator (space by-default).
*
* @param defaultDelimiter a string to join the array elements by, default is space
* @param defaultSeparator a string to join the array elements by, default is space
*/
export const ArraysPlugin = (defaultDelimiter = ' ') => createPlugin('ArraysPlugin', match, {
export const ArraysPlugin = (defaultSeparator = ' ') => createPlugin('ArraysPlugin', match, {
translate(
referenceParams: Record<string, unknown[]>,
delimiter: string | ((arr: string[], dDelimiter: string) => string) = defaultDelimiter
separator: string | ((arr: string[], dSeparator: string) => string) = defaultSeparator
) {
const startsWithIndex = /^.*?:/;

Expand Down Expand Up @@ -135,11 +135,11 @@ export const ArraysPlugin = (defaultDelimiter = ' ') => createPlugin('ArraysPlug
return [...arr, ...processReference(refParamK)];
}, []);

if (typeof delimiter === 'string') {
return result.join(delimiter);
if (typeof separator === 'string') {
return result.join(separator);
}

return delimiter(result, defaultDelimiter);
return separator(result, defaultSeparator);

function normalizeRefs(referenceMap: Record<string, unknown>, referenceKey: string) {
return Array.isArray(referenceMap[referenceKey])
Expand Down
7 changes: 2 additions & 5 deletions packages/plugins/defaults/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ This package also exports everything from the included plugins.

```ts
import { createTranslator } from 'intl-schematic';
import { defaultPlugins, defaultProcessors } from '@intl-schematic/plugin-defaults';
import { defaultPlugins } from '@intl-schematic/plugin-defaults';

const getLocale = () => new Intl.Locale(navigator.language);

// Notice the plugins array parameter
const t = createTranslator(getDocument, defaultPlugins(
getLocale
defaultProcessors
));
const t = createTranslator(getDocument, defaultPlugins(getLocale));
```

Then use according to the instructions of included plugins.
2 changes: 1 addition & 1 deletion packages/plugins/functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { FunctionsPlugin } from '@intl-schematic/plugin-functions';
const t = createTranslator(getDocument, [FunctionsPlugin]);
```

### Use a translator function
### Use the translator function

```ts
console.log(t('hello', 'Bob')); // `Hello, Bob!`
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/nested/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { NestedKeysPlugin } from '@intl-schematic/plugin-nested';
const t = createTranslator(getDocument, [NestedKeysPlugin]);
```

### Use a translator function
### Use the translator function

```ts
console.log(t('hello', 'world')); // `Hello, world!`
Expand Down
6 changes: 3 additions & 3 deletions packages/plugins/processors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Adds the ability to use custom processors for records in translation documents.
- [Usage](#usage)
- [Define a translation document factory](#define-a-translation-document-factory)
- [Create a translator function (`t()`)](#create-a-translator-function-t)
- [Use a translator function](#use-a-translator-function)
- [Use the translator function](#use-a-translator-function)
- [Document record format](#document-record-format)
- [Processors](#processors)
- [Processor API](#processor-api)
Expand All @@ -29,7 +29,7 @@ const getDocument = () => ({
trailingZeroDisplay: "stripIfInteger"
},

// OR use a full processor name for clarity
// OR use the full processor name for clarity
'intl/number': {
// Intl.NumberFormat options
style: "currency",
Expand Down Expand Up @@ -67,7 +67,7 @@ const t = createTranslator(getDocument, [
]);
```

### Use a translator function
### Use the translator function

```ts
console.log(t('birthday', new Date(1997, 7, 9))); // Aug 9, 1997
Expand Down

0 comments on commit 66ce273

Please sign in to comment.