Update locale serving backend
This commit is contained in:
parent
1e4ecc7e6e
commit
eb4cda9f64
4 changed files with 27 additions and 26 deletions
|
@ -1,39 +1,41 @@
|
||||||
import { LOCALE_URI } from "@saleor/config";
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { IntlProvider } from "react-intl";
|
import { IntlProvider } from "react-intl";
|
||||||
import urlJoin from "url-join";
|
|
||||||
|
|
||||||
export type LocaleContextType = string;
|
export type LocaleContextType = string;
|
||||||
export const LocaleContext = React.createContext<LocaleContextType>("en");
|
export const LocaleContext = React.createContext<LocaleContextType>("en");
|
||||||
|
|
||||||
const { Consumer: LocaleConsumer, Provider: RawLocaleProvider } = LocaleContext;
|
const { Consumer: LocaleConsumer, Provider: RawLocaleProvider } = LocaleContext;
|
||||||
|
|
||||||
const LocaleProvider: React.FC = ({ children }) => {
|
enum Locale {
|
||||||
const [localeIndex, setLocaleIndex] = React.useState(0);
|
EN = "en",
|
||||||
const [messages, setMessages] = React.useState({});
|
EN_GB = "en-gb",
|
||||||
|
EN_US = "en-us"
|
||||||
|
}
|
||||||
|
|
||||||
const locale = navigator.languages[localeIndex];
|
type LocaleMessages = Record<string, string>;
|
||||||
|
const localeData: Record<Locale, LocaleMessages> = {
|
||||||
|
[Locale.EN]: {},
|
||||||
|
[Locale.EN_GB]: {},
|
||||||
|
[Locale.EN_US]: {}
|
||||||
|
};
|
||||||
|
|
||||||
React.useEffect(() => {
|
function getMatchingLocale(): Locale {
|
||||||
async function fetchLocale() {
|
const localeEntries = Object.entries(Locale);
|
||||||
if (locale) {
|
|
||||||
const res = await fetch(urlJoin(LOCALE_URI, `${locale}.json`), {
|
for (const preferredLocale of navigator.languages) {
|
||||||
credentials: "same-origin",
|
for (const localeEntry of localeEntries) {
|
||||||
mode: "cors"
|
if (localeEntry[1].toLowerCase() === preferredLocale.toLowerCase()) {
|
||||||
});
|
return Locale[localeEntry[0]];
|
||||||
if (res.ok) {
|
|
||||||
const localeData = await res.json();
|
|
||||||
setMessages(localeData);
|
|
||||||
} else {
|
|
||||||
setLocaleIndex(localeIndex + 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fetchLocale();
|
}
|
||||||
}, [localeIndex]);
|
}
|
||||||
|
|
||||||
|
const LocaleProvider: React.FC = ({ children }) => {
|
||||||
|
const [locale] = React.useState(getMatchingLocale());
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<IntlProvider locale={locale} messages={messages} key={locale}>
|
<IntlProvider locale={locale} messages={localeData[locale]} key={locale}>
|
||||||
<RawLocaleProvider value={locale}>{children}</RawLocaleProvider>
|
<RawLocaleProvider value={locale}>{children}</RawLocaleProvider>
|
||||||
</IntlProvider>
|
</IntlProvider>
|
||||||
);
|
);
|
||||||
|
|
|
@ -3,7 +3,6 @@ import { ListSettings, ListViews } from "./types";
|
||||||
|
|
||||||
export const APP_MOUNT_URI = process.env.APP_MOUNT_URI || "/";
|
export const APP_MOUNT_URI = process.env.APP_MOUNT_URI || "/";
|
||||||
export const API_URI = process.env.API_URI || "/graphql/";
|
export const API_URI = process.env.API_URI || "/graphql/";
|
||||||
export const LOCALE_URI = process.env.LOCALE_URI || "/";
|
|
||||||
|
|
||||||
export const DEFAULT_INITIAL_SEARCH_DATA: SearchQueryVariables = {
|
export const DEFAULT_INITIAL_SEARCH_DATA: SearchQueryVariables = {
|
||||||
after: null,
|
after: null,
|
||||||
|
|
|
@ -11,7 +11,8 @@
|
||||||
"paths": {
|
"paths": {
|
||||||
"@assets/*": ["assets/*"],
|
"@assets/*": ["assets/*"],
|
||||||
"@saleor/*": ["src/*"]
|
"@saleor/*": ["src/*"]
|
||||||
}
|
},
|
||||||
|
"resolveJsonModule": true
|
||||||
},
|
},
|
||||||
"exclude": ["node_modules"]
|
"exclude": ["node_modules"]
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,8 +21,7 @@ const htmlWebpackPlugin = new HtmlWebpackPlugin({
|
||||||
});
|
});
|
||||||
const environmentPlugin = new webpack.EnvironmentPlugin([
|
const environmentPlugin = new webpack.EnvironmentPlugin([
|
||||||
"APP_MOUNT_URI",
|
"APP_MOUNT_URI",
|
||||||
"API_URI",
|
"API_URI"
|
||||||
"LOCALE_URI"
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const dashboardBuildPath = "build/dashboard/";
|
const dashboardBuildPath = "build/dashboard/";
|
||||||
|
|
Loading…
Reference in a new issue