Skip to content

Commit

Permalink
fix legacy strings loading
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-burel committed Jun 3, 2024
1 parent 65b680e commit 4e70e1d
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 14 deletions.
5 changes: 4 additions & 1 deletion shared/i18n/server/fetch-locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ export async function getLocaleDict({ localeId, contexts }: {
// TODO: assess if it's the right decisio
dict[item.key] = item //tHtml || t
})
const localeParsed: LocaleParsed = { ...localeDef, dict }
const localeParsed: LocaleParsed = {
...localeDef,
strings, dict
}
return { locale: localeParsed }
}
/**
Expand Down
5 changes: 4 additions & 1 deletion shared/i18n/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export interface Translation {
*/
export interface Locale {
id: string
// TODO: remove this to clarify LocaleDefinition, LocaleStrings, LocaleParsed as dict
/**
* @deprecated Prefer using "LocaleParsed" dict object
* This is the structure returned by the API but it shouldn't be used after parsing
*/
strings?: Translation[]
/**
* TODO: this value was not present in the type def here but is expected by Popover
Expand Down
1 change: 1 addition & 0 deletions shared/react-i18n/TranslationMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// TODO: create Portal might be usable to avoid having many React roots?
// but need to play around with declaratively rendering each portal in a loop
// import { createPortal } from "react-dom"
"use client"
import { createRoot } from "react-dom/client"
import { DATA_TOKEN_ATTR, TOKEN_DATASET } from "@devographics/i18n";

Expand Down
1 change: 1 addition & 0 deletions shared/react-i18n/i18nContext.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use client"
import React, { createContext, useContext, useMemo } from 'react'
import { makeTranslatorFunc, type Locale, type StringTranslator } from '@devographics/i18n'

Expand Down
21 changes: 12 additions & 9 deletions surveyform/src/app/[lang]/(mainLayout)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DEFAULT_REVALIDATE_S } from "~/app/revalidation";
import { setLocaleIdServerContext } from "~/i18n/rsc-context";
import { NextPageParams } from "~/app/typings";
import { filterClientSideStrings } from "@devographics/i18n/server";
import { I18nContextProvider } from "@devographics/react-i18n";

// revalidating is important so we get fresh values from the cache every now and then without having to redeploy
export const revalidate = DEFAULT_REVALIDATE_S;
Expand All @@ -23,16 +24,18 @@ const IndexPage = async ({ params }: NextPageParams<{ lang: string }>) => {
const { locale, localeId, error } = await rscLocaleFromParams(params)
if (error) return <div>Can't load translations</div>
const tokenExprs = ["general.open_surveys"]
const clientSideStrings = filterClientSideStrings<{}>(locale, tokenExprs, {})
console.log({ clientSideStrings })

const clientSideLocale = filterClientSideStrings<{}>(locale, tokenExprs, {})
return (
<RSCFetch
fetch={async () => rscFetchSurveysMetadata({ shouldThrow: false })}
render={({ data: surveys }) => (
<Surveys surveys={surveys} />
)}
/>
<I18nContextProvider
locale={clientSideLocale}
>
<RSCFetch
fetch={async () => rscFetchSurveysMetadata({ shouldThrow: false })}
render={({ data: surveys }) => (
<Surveys surveys={surveys} />
)}
/>
</I18nContextProvider>
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import Nav from "./Nav";
import { EditionMetadata } from "@devographics/types";

const Header = (props) => {
const Header = (props: { edition?: EditionMetadata }) => {
return (
<div className="header">
<Nav {...props} />
Expand Down
2 changes: 1 addition & 1 deletion surveyform/src/components/common/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { EditionMetadata } from "@devographics/types";
import { getEditionHomePath } from "~/lib/surveys/helpers/getEditionHomePath";
import { useLocaleContext } from "~/i18n/context/LocaleContext";

const Navigation = ({ edition }: { edition: EditionMetadata }) => {
const Navigation = ({ edition }: { edition?: EditionMetadata }) => {
const { locale } = useLocaleContext();
return (
<div className="nav-wrapper">
Expand Down
7 changes: 6 additions & 1 deletion surveyform/src/i18n/context/LocaleContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
IntlContextProvider,
StringsRegistry,
} from "@devographics/react-i18n-legacy";
import {
I18nContextProvider
} from "@devographics/react-i18n"
import { useRouter } from "next/navigation";
import { useCookies } from "react-cookie";
import { LOCALE_COOKIE_NAME } from "../cookie";
Expand Down Expand Up @@ -63,6 +66,8 @@ export const useSetLocale = (updateUser?: any) => {
/**
* Provide methods to get/set the current locale
* + initialize an IntlProvider
*
* @deprecated use @devographics/React-i18n
* @param props
* @returns
*/
Expand Down Expand Up @@ -90,7 +95,7 @@ export const LocaleContextProvider = (props: {

const stringsRegistry = new StringsRegistry("en-US");
// @ts-ignore
stringsRegistry.addStrings(localeId, localeStrings.strings);
stringsRegistry.addStrings(localeId, localeStrings.dict);
return (
// NOTE: IntlContextProvider is in charge of merging strings with a previously existing parent
<IntlContextProvider stringsRegistry={stringsRegistry} localeId={localeId}>
Expand Down

0 comments on commit 4e70e1d

Please sign in to comment.