Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/__stories__/useT.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,19 @@ storiesOf('useT', module)
.add('Without provider', () =>
<Demo/>
)
.add('Initial language not default, loaded with loader', () =>
<Provider
defaultLocale='en'
locale='fr'
map={{
en: {
main: {Hello: 'Hello', welcome: 'Welcome!'}
},
}}
loader={async (locale, ns) => {
return {Hello: 'Bonjour', welcome: 'Lala!'};
}}
>
<Demo />
</Provider>
)
7 changes: 5 additions & 2 deletions src/createTranslations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export const createTranslations = (ns: string = 'main'): Result => {
// Normalize translation map.
if (!map[defaultLocale]) map[defaultLocale] = {[ns]: {}};
else if (!map[defaultLocale][ns]) map[defaultLocale][ns] = {};
if (!map[locale]) map[locale] = {[ns]: {}};
else if (!map[locale][ns]) map[locale][ns] = {};

this.state = {
locale,
Expand All @@ -41,6 +39,10 @@ export const createTranslations = (ns: string = 'main'): Result => {
setLocale: this.setLocale,
createT: this.createT,
};

if (locale !== defaultLocale) {
this.load(locale, ns);
}
}

load = async (locale: string, ns: string) => {
Expand All @@ -58,6 +60,7 @@ export const createTranslations = (ns: string = 'main'): Result => {
};

setLocale = (locale: string) => {
if (locale === this.state.locale) return;
if (!this.state.map[locale])
this.state.map[locale] = {};
this.setState({locale});
Expand Down