Skip to content

Commit

Permalink
feat(js sdk): Prepare for Next.js TG-272 (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCizmar authored Sep 8, 2021
1 parent 5811f90 commit 7c75a1b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/Tolgee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ describe('Tolgee', () => {
});

test('will return proper initialLoading', () => {
tolgee.properties.config.mode = 'production';
tolgee.properties.config.preloadFallback = true;
tolgee.properties.currentLanguage = 'cs';
tolgee.properties.config.staticData = {
Expand All @@ -267,6 +268,8 @@ describe('Tolgee', () => {
tolgee.properties.config.preloadFallback = true;
tolgee.properties.config.staticData.en = {};
expect(tolgee.initialLoading).toEqual(false);
tolgee.properties.config.mode = 'development';
expect(tolgee.initialLoading).toEqual(true);
});

describe('lang setter', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/Tolgee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export class Tolgee {
* fetching so initial loading is not needed at all.
*/
get initialLoading(): boolean {
if (this.properties?.config?.mode !== 'production') {
return true;
}

const currentLang = this.properties.currentLanguage;
const fallbackLang = this.properties.config.fallbackLanguage;
const fallbackPreloading = this.properties.config.preloadFallback;
Expand Down
7 changes: 5 additions & 2 deletions packages/react/src/T.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ type TProps = {
export const T: FunctionComponent<TProps> = (props: TProps) => {
const context = useTolgeeContext();

const noWrap = typeof window !== 'undefined' ? props.noWrap : true;

const [translated, setTranslated] = useState(
context.tolgee.instant(props.children, props.parameters, props.noWrap, true)
context.tolgee.instant(props.children, props.parameters, noWrap, true)
);

const translate = () =>
context.tolgee
.translate(props.children, props.parameters, props.noWrap)
.translate(props.children, props.parameters, noWrap)
.then((t) => {
setTranslated(t);
});
Expand Down
7 changes: 7 additions & 0 deletions packages/react/src/TolgeeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export const TolgeeProvider: FunctionComponent<TolgeeProviderProps> = (

const [loading, setLoading] = useState(tolgee.initialLoading);

useEffect(() => {
if (config.forceLanguage !== undefined) {
tolgee.properties.config.forceLanguage = config.forceLanguage;
tolgee.lang = config.forceLanguage;
}
}, [config.forceLanguage]);

useEffect(() => {
tolgee.run().then(() => setLoading(false));

Expand Down

0 comments on commit 7c75a1b

Please sign in to comment.