Skip to content

Commit

Permalink
feat: use Localization object for translations (#388)
Browse files Browse the repository at this point in the history
* RM#74955
  • Loading branch information
gca-axelor authored Feb 6, 2024
1 parent 7af9497 commit 6d0d62a
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/auth/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
*/

export {searchCompany} from './company-api';
export {searchLanguage} from './language-api';
export {searchLocalization} from './localization-api';
export {getLoggedUser, postUser} from './user-api';
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import {axiosApiProvider} from '../../apiProviders';
import {createStandardSearch} from '../../apiProviders';

export async function searchLanguage() {
return axiosApiProvider.get({
url: '/ws/rest/com.axelor.apps.base.db.Language/',
export async function searchLocalization() {
return createStandardSearch({
model: 'com.axelor.apps.base.db.Localization',
criteria: [],
fieldKey: 'auth_localization',
page: 0,
});
}
2 changes: 1 addition & 1 deletion packages/core/src/auth/features/asyncFunctionsIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

export {fetchCompanies} from './companySlice';
export {uploadTranslations} from './configSlice';
export {fetchLanguages} from './languageSlice';
export {fetchLocalizations} from './localizationSlice';
export {
fetchActiveUser,
updateActiveUser,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/auth/features/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@

export {companyReducer as company} from './companySlice';
export {configReducer as config} from './configSlice';
export {languageReducer as language} from './languageSlice';
export {localizationReducer as localization} from './localizationSlice';
export {userReducer as user} from './userSlice';
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

import {createAsyncThunk, createSlice} from '@reduxjs/toolkit';
import {handlerApiCall} from '../../apiProviders/utils';
import {searchLanguage} from '../api/language-api';
import {searchLocalization} from '../api/localization-api';

export const fetchLanguages = createAsyncThunk(
'language/fetchLanguage',
export const fetchLocalizations = createAsyncThunk(
'localization/fetchLocalizations',
async function (data = {}, {getState}) {
return handlerApiCall({
fetchFunction: searchLanguage,
fetchFunction: searchLocalization,
data,
action: 'Auth_SliceAction_FetchLanguages',
action: 'Auth_SliceAction_FetchLocalization',
getState,
responseOptions: {isArrayResponse: true},
});
Expand All @@ -35,21 +35,21 @@ export const fetchLanguages = createAsyncThunk(

const initialState = {
loading: false,
languageList: [],
localizationList: [],
};

const languageSlice = createSlice({
name: 'language',
const localizationSlice = createSlice({
name: 'localization',
initialState,
extraReducers: builder => {
builder.addCase(fetchLanguages.pending, state => {
builder.addCase(fetchLocalizations.pending, state => {
state.loading = true;
});
builder.addCase(fetchLanguages.fulfilled, (state, action) => {
builder.addCase(fetchLocalizations.fulfilled, (state, action) => {
state.loading = false;
state.languageList = action.payload;
state.localizationList = action.payload;
});
},
});

export const languageReducer = languageSlice.reducer;
export const localizationReducer = localizationSlice.reducer;
2 changes: 1 addition & 1 deletion packages/core/src/auth/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"User_Language": "Language",
"User_Theme": "Theme",
"User_NoAppUser": "The user could not be fetched. Please verify that you have the necessary permissions or contact your administrator.",
"Auth_SliceAction_FetchLanguages": "fetch languages",
"Auth_SliceAction_FetchLocalization": "fetch localizations",
"Auth_SliceAction_FetchCompanies": "fetch companies",
"Auth_SliceAction_FetchActiveUser": "fetch active user",
"Auth_SliceAction_UpdateActiveUser": "update active user"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/auth/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"User_Language": "Langage",
"User_Theme": "Thème",
"User_NoAppUser": "L'utilisateur n'a pas pu être récupéré. Veuillez vérifier que vous avez les permissions nécessaires ou contactez votre administrateur.",
"Auth_SliceAction_FetchLanguages": "récupération des langages",
"Auth_SliceAction_FetchLocalization": "récupération des localisations",
"Auth_SliceAction_FetchCompanies": "récupération des sociétés",
"Auth_SliceAction_FetchActiveUser": "récupération de l'utilisateur actif",
"Auth_SliceAction_UpdateActiveUser": "mise à jour de l'utilisateur actif"
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/auth/models/objectFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export const auth_modelAPI: ObjectFields = {
currency: schemaContructor.subObject('symbol'),
}),
),
localization: schemaContructor.subObject().concat(
schemaContructor.object({
language: schemaContructor.subObject(),
}),
),
activeTeam: schemaContructor.subObject(),
workshopStockLocation: schemaContructor.subObject(),
roles: schemaContructor.array(),
Expand All @@ -39,4 +44,13 @@ export const auth_modelAPI: ObjectFields = {
language: schemaContructor.string(),
todayDateT: schemaContructor.string(),
}),
auth_localization: schemaContructor.object({
code: schemaContructor.string(),
name: schemaContructor.string(),
language: schemaContructor.subObject().concat(
schemaContructor.object({
code: schemaContructor.number(),
}),
),
}),
};
22 changes: 13 additions & 9 deletions packages/core/src/auth/screens/UserScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
useTranslator,
} from '../../index';
import {fetchCompanies} from '../features/companySlice';
import {fetchLanguages} from '../features/languageSlice';
import {fetchLocalizations} from '../features/localizationSlice';
import {
changeActiveCompany,
updateActiveUser,
Expand All @@ -55,7 +55,7 @@ const UserScreen = ({children}) => {

const {companyList} = useSelector(state => state.company);
const {userId} = useSelector(state => state.auth);
const {languageList} = useSelector(state => state.language);
const {localizationList} = useSelector(state => state.localization);
const {base: baseConfig} = useSelector(state => state.appConfig);
const {loadingUser, user, isUser, canModifyCompany} = useSelector(
state => state.user,
Expand All @@ -67,7 +67,7 @@ const UserScreen = ({children}) => {
useEffect(() => {
fetchUser();
dispatch(fetchCompanies());
dispatch(fetchLanguages());
dispatch(fetchLocalizations());
}, [dispatch, fetchUser, userId]);

useEffect(() => {
Expand Down Expand Up @@ -112,9 +112,13 @@ const UserScreen = ({children}) => {
);

const updateLanguage = useCallback(
language => {
localization => {
dispatch(
updateActiveUser({id: user.id, language, version: user.version}),
updateActiveUser({
id: user.id,
localization: {id: localization},
version: user.version,
}),
);
},
[dispatch, user],
Expand Down Expand Up @@ -165,13 +169,13 @@ const UserScreen = ({children}) => {
/>
)}
{children}
{languageList?.length > 1 && (
{localizationList?.length > 1 && (
<Picker
title={I18n.t('User_Language')}
defaultValue={user.language}
listItems={languageList}
defaultValue={user.localization?.id}
listItems={localizationList}
labelField="name"
valueField="code"
valueField="id"
onValueChange={updateLanguage}
emptyValue={false}
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/i18n/component/Translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {useSelector} from 'react-redux';

export const selectLanguage = createSelector(
state => state?.user,
userState => userState?.user?.language,
userState => userState?.user?.localization?.language?.code,
);

const DEFAULT_NAMESPACE = 'translation';
Expand Down

0 comments on commit 6d0d62a

Please sign in to comment.