Skip to content

Commit

Permalink
Merge pull request #9 from sveltekit-i18n/feat/preprocess
Browse files Browse the repository at this point in the history
Introduce `preprocess` config prop
  • Loading branch information
jarda-svoboda authored Jul 6, 2023
2 parents 4fc3dd2 + e5bcf9c commit 8768fcf
Show file tree
Hide file tree
Showing 15 changed files with 4,152 additions and 3,922 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
name: Tests

on: [push, workflow_dispatch]
on:
workflow_dispatch:
pull_request:
types: [synchronize]

jobs:
Tests:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [16]
node-version: [18]
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ const config = ({
export const { t, locale, locales, loading, loadTranslations } = new i18n(config);
```

...load your translations in `__layout.svelte`...
...load your translations in `+layout.js`...

```svelte
<script context="module">
```js
import { locale, loadTranslations } from '$lib/translations';

export const load = async ({ url }) => {
Expand All @@ -99,7 +98,6 @@ export const { t, locale, locales, loading, loadTranslations } = new i18n(config

return {};
}
</script>
```

...and include your translations within pages and components.
Expand Down
40 changes: 35 additions & 5 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@


## Config
### `parser`: __[Parser.T](https://github.com/sveltekit-i18n/base/blob/master/src/types.ts#L97-L99)__
This property defines translation syntax you want to use. For more, see [Parsers](https://github.com/sveltekit-i18n/parsers#readme).

### `translations`?: __[Translations.T](https://github.com/sveltekit-i18n/base/blob/master/src/types.ts#L127)__
### `translations`?: __[Translations.T](https://github.com/sveltekit-i18n/base/blob/master/src/types.ts)__
This property defines translations, which should be in place before `loaders` will trigger. It's useful for synchronous translations (e.g. locally defined language names which are same for all language mutations).

### `loaders`?: __[Loader.LoaderModule[]](https://github.com/sveltekit-i18n/base/blob/master/src/types.ts#L69-L74)__
### `loaders`?: __[Loader.LoaderModule[]](https://github.com/sveltekit-i18n/base/blob/master/src/types.ts)__
You can use `loaders` to define your asyncronous translation load. All loaded data are stored so loader is triggered only once – in case there is no previous version of the translation. It can get refreshed according to `config.cache`.\
Each loader can include:

Expand All @@ -23,6 +21,38 @@ Each loader can include:

`routes`?: __Array<string | RegExp>__ – can define routes this loader should be triggered for. You can use Regular expressions too. For example `[/\/.ome/]` will be triggered for `/home` and `/rome` route as well (but still only once). Leave this `undefined` in case you want to load this module with any route (useful for common translations).

### `preprocess`?: __'full' | 'preserveArrays' | 'none' | (input: Translations.Input) => any__
Defines a preprocess strategy or a custom preprocess function. Preprocessor runs immediately after the translation data load. This is set to `'full'` by default.

Examples for input:
```json
{"a": {"b": [{"c": {"d": 1}}, {"c": {"d": 2}}]}}
```

`'full'` (default) setting will result in:
```json
{"a.b.0.c.d": 1, "a.b.1.c.d": 2}
```

`'preserveArrays'` in:
```json
{"a.b": [{"c.d": 1}, {"c.d": 2}]}
```

`'none'` (nothing's changed):
```json
{"a": {"b": [{"c": {"d": 1}}, {"c": {"d": 2}}]}}
```

Custom preprocess function `(input) => JSON.parse(JSON.stringify(input).replace('1', '"🦄"'))` will output:

```json
{"a": {"b": [{"c": {"d": "🦄"}}, {"c": {"d": 2}}]}}
```

### `parser`: __[Parser.T](https://github.com/sveltekit-i18n/base/blob/master/src/types.ts)__
This property defines translation syntax you want to use. For more, see [Parsers](https://github.com/sveltekit-i18n/parsers#readme).

### `initLocale`?: __string__
If you set this property, translations will be initialized immediately using this locale.

Expand All @@ -45,7 +75,7 @@ You can manage log level using this property (default: `'warn'`).
### `log.prefix`?: __string__
You can prefix output logs using this property (default: `'[i18n]: '`).

### `log.logger`?: __[Logger.T](https://github.com/sveltekit-i18n/base/blob/b488f34b2c160b62943968929c9e6e1ee642c5e8/src/types.ts#L20-L22)__
### `log.logger`?: __[Logger.T](https://github.com/sveltekit-i18n/base/blob/master/src/types.ts)__
You can setup your custom logger using this property (default: `console`).

## Instance methods and properties
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default ({
preset: 'ts-jest',
preset: 'ts-jest/presets/default-esm',
testEnvironment: 'node',
});
Loading

0 comments on commit 8768fcf

Please sign in to comment.