@applica-software-guru/react-admin 1.5.331 → 1.5.332

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,6 +20,7 @@ type ICreateI18nProviderData = {
20
20
  };
21
21
  type ICreateJsonI18nProviderData = {
22
22
  path: string;
23
+ locales: Locale[];
23
24
  defaultLocale: string;
24
25
  };
25
26
  type IPolyglotOptions = {
@@ -100,18 +101,11 @@ function createI18nProvider(data: ICreateI18nProviderData): Promise<any> {
100
101
  * @returns {Promise<I18nProvider>} - The I18nProvider
101
102
  */
102
103
  function createJsonI18nProvider(data: ICreateJsonI18nProviderData): Promise<I18nProvider> {
103
- const { defaultLocale, path } = data;
104
- const defaultLanguages: Array<Locale> = [{ locale: defaultLocale, name: defaultLocale }];
104
+ const { locales, defaultLocale, path } = data;
105
105
  const headers = new Headers({ Accept: 'application/json', 'Content-Type': 'application/json' });
106
- function getLanguages(): Promise<Locale> {
107
- return fetch(`${path}/languages.json`, { headers: headers, method: 'get' })
108
- .then((response) => response.json())
109
- .catch(() => {
110
- return Promise.resolve(defaultLanguages);
111
- });
112
- }
113
- function getMessages(): Promise<ITranslations> {
114
- return fetch(`${path}/messages.json`, { headers: headers, method: 'get' })
106
+
107
+ function getMessages(locale: Locale): Promise<ITranslations> {
108
+ return fetch(`${path}/${locale.locale}.json`, { headers: headers, method: 'get' })
115
109
  .then((response) => response.json())
116
110
  .then((response: Array<IMessage>): ITranslations => {
117
111
  return response.reduce(
@@ -126,15 +120,26 @@ function createJsonI18nProvider(data: ICreateJsonI18nProviderData): Promise<I18n
126
120
  );
127
121
  });
128
122
  }
129
- return Promise.all([getLanguages(), getMessages()]).then((responses) => {
130
- const [languages, messages] = responses;
131
- function getMessages(locale: string): TranslationMessages {
132
- return _.get(messages, locale, {}) as TranslationMessages;
133
- }
134
- return polyglotI18nProvider(getMessages, defaultLocale, languages, {
135
- allowMissing: true
123
+ return Promise.all(locales.map(getMessages))
124
+ .then((responses: ITranslations[]) =>
125
+ _.reduce(
126
+ responses,
127
+ (acc, messages) =>
128
+ ({
129
+ ...acc,
130
+ ...messages
131
+ } as ITranslations),
132
+ {}
133
+ )
134
+ )
135
+ .then((messages) => {
136
+ function getMessages(locale: string): TranslationMessages {
137
+ return _.get(messages, locale, {}) as TranslationMessages;
138
+ }
139
+ return polyglotI18nProvider(getMessages, defaultLocale, locales, {
140
+ allowMissing: true
141
+ });
136
142
  });
137
- });
138
143
  }
139
144
 
140
145
  export type { ICreateI18nProviderData };
@@ -20,7 +20,17 @@ const dataProvider = new ApplicaDataProvider({
20
20
  attachmentsParser: createAttachmentsParser(),
21
21
  HttpErrorClass: HttpError
22
22
  });
23
- const i18nProvider = createI18nProvider({ apiUrl: API_URL, allowMissing: true, defaultLocale: 'it' });
23
+ const useJson = false;
24
+ const i18nProvider = useJson
25
+ ? createJsonI18nProvider({
26
+ path: '/i18n',
27
+ defaultLocale: 'it',
28
+ locales: [
29
+ { locale: 'it', name: 'Italiano' },
30
+ { locale: 'en', name: 'English' }
31
+ ]
32
+ })
33
+ : createI18nProvider({ apiUrl: API_URL, allowMissing: true, defaultLocale: 'it' });
24
34
  function App() {
25
35
  return (
26
36
  <ApplicaAdmin
@@ -44,6 +54,7 @@ function App() {
44
54
  oauth={{
45
55
  google: { clientId: 'YOUR_CLIENT_ID' }
46
56
  }}
57
+ enableLocaleSwitcher
47
58
  >
48
59
  <CustomRoutes>
49
60
  <Route path="/custom-page" element={<CustomPage />} />
@@ -1 +0,0 @@
1
- [{ "locale": "en", "name": "English" }]
Binary file
@@ -1 +0,0 @@
1
- [{ "locale": "en", "name": "English" }]