Skip to content

Commit

Permalink
Expose instead of
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Apr 8, 2024
1 parent 60af5db commit 6fbe517
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .changeset/poor-carrots-argue.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@withease/i18next': minor
---

Add _Store_ `$language` and _EventCallable_ `changeLanguage` to integration
Add _Store_ `$language` and _Effect_ `changeLanguageFx` to integration
8 changes: 4 additions & 4 deletions apps/website/docs/i18next/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,19 @@ const { $language } = createI18nextIntegration({
});
```

#### `changeLanguage` <Badge text="since v23.2.0" />
#### `changeLanguageFx` <Badge text="since v23.2.0" />

An [_EventCallable_](https://effector.dev/en/api/effector/event/) that can be called with a language code to change the current language.
An [_Effect_](https://effector.dev/en/api/effector/effect/) that can be called with a language code to change the current language.

```ts
const { changeLanguage } = createI18nextIntegration({
const { changeLanguageFx } = createI18nextIntegration({
/* ... */
});

sample({
clock: someButtonClicked,
fn: () => 'en',
target: changeLanguage,
target: changeLanguageFx,
});
```

Expand Down
30 changes: 13 additions & 17 deletions packages/i18next/src/integration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
type Event,
type Store,
type EventCallable,
type Effect,
attach,
combine,
createEffect,
Expand Down Expand Up @@ -30,7 +30,7 @@ type I18nextIntegration = {
translated: Translated;
$isReady: Store<boolean>;
$language: Store<string | null>;
changeLanguage: EventCallable<string>;
changeLanguageFx: Effect<string, void, unknown>;
reporting: {
missingKey: Event<MissinKeyReport>;
};
Expand Down Expand Up @@ -85,7 +85,16 @@ export function createI18nextIntegration({

const $language = createStore<string | null>(null, { serialize: 'ignore' });

const changeLanguage = createEvent<string>();
const changeLanguageFx = attach({
source: $instance,
async effect(instance, nextLangauge: string) {
if (!instance) {
return;
}

await instance.changeLanguage(nextLangauge);
},
});

// -- End of public API

Expand All @@ -107,19 +116,6 @@ export function createI18nextIntegration({
target: $language,
});

const changeLanguageFx = attach({
source: $instance,
async effect(instance, nextLangauge: string) {
if (!instance) {
return;
}

await instance.changeLanguage(nextLangauge);
},
});

sample({ clock: changeLanguage, target: changeLanguageFx });

sample({
clock: instanceInitialized,
fn: () => true,
Expand Down Expand Up @@ -261,7 +257,7 @@ export function createI18nextIntegration({
$isReady,
$t,
$language,
changeLanguage,
changeLanguageFx,
translated: (firstArg, ...args: any[]) => {
if (typeof firstArg === 'string') {
return translatedWithVariables(firstArg, args[0]);
Expand Down
13 changes: 7 additions & 6 deletions packages/i18next/src/language.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createInstance } from 'i18next';

import { createI18nextIntegration } from './integration';

describe('integration.$language/changeLanguage', () => {
describe('integration.$language/changeLanguageFx', () => {
test('change language', async () => {
const setup = createEvent();

Expand All @@ -16,10 +16,11 @@ describe('integration.$language/changeLanguage', () => {
lng: 'th',
});

const { $language, changeLanguage, translated } = createI18nextIntegration({
instance,
setup,
});
const { $language, changeLanguageFx, translated } =
createI18nextIntegration({
instance,
setup,
});

const $val = translated('common:key');

Expand All @@ -34,7 +35,7 @@ describe('integration.$language/changeLanguage', () => {
expect(scope.getState($language)).toBe('th');
expect(scope.getState($val)).toBe('th value');

await allSettled(changeLanguage, { scope, params: 'en' });
await allSettled(changeLanguageFx, { scope, params: 'en' });

// After change
expect(scope.getState($language)).toBe('en');
Expand Down
6 changes: 3 additions & 3 deletions packages/i18next/src/t.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('integration.$t', () => {

const setup = createEvent();

const { $t } = createI18nextIntegration({
const { $t, changeLanguageFx } = createI18nextIntegration({
instance,
setup,
});
Expand All @@ -131,7 +131,7 @@ describe('integration.$t', () => {

expect(scope.getState($result)).toBe('Sawa dee');

instance.changeLanguage('en');
await allSettled(changeLanguageFx, { params: 'en', scope });

await allSettled(scope);
expect(scope.getState($result)).toBe('Hello');
Expand Down Expand Up @@ -240,7 +240,7 @@ describe('integration.$t', () => {

await allSettled(teardown, { scope });

instance.changeLanguage('en');
await allSettled(changeLanguageFx, { params: 'en', scope });

await allSettled(scope);
expect(scope.getState($result)).toBe('Sawa dee');
Expand Down

0 comments on commit 6fbe517

Please sign in to comment.