Skip to content

Commit

Permalink
Merge pull request #206 from k2maan/dxp-components/#196
Browse files Browse the repository at this point in the history
Implemented: locale update through user profile (#196)
  • Loading branch information
ravilodhi authored Oct 12, 2023
2 parents 1023843 + bb5a359 commit d984c85
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/components/LanguageSwitcher.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const userStore = useUserStore();
const locales = computed(() => userStore.getLocaleOptions);
const currentLocale = computed(() => userStore.getLocale);
const setLocale = (locale: any) => {
const setLocale = (locale: string) => {
userStore.setLocale(locale)
}
</script>
2 changes: 1 addition & 1 deletion src/components/Login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default defineComponent({
const userStore = useUserStore()
// to access baseUrl as we store only OMS in DXP
const appState = appContext.config.globalProperties.$store
await userStore.getPreference(token, appState.getters['user/getBaseUrl'])
await userStore.setLocale(appState.getters['user/getUserProfile'].userLocale)

// check if firebase configurations are there
if (notificationContext.appFirebaseConfig) {
Expand Down
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export let dxpComponents = {
// Creating an instance of the i18n and translate function for translating text
i18n = createI18n({
legacy: false,
locale: process.env.VUE_APP_I18N_LOCALE || 'en',
fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || 'en',
locale: process.env.VUE_APP_I18N_LOCALE || 'en-US',
fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || 'en-US',
messages: options.localeMessages
})

Expand All @@ -62,8 +62,7 @@ export let dxpComponents = {
imageContext.defaultImgUrl = options.defaultImgUrl
shopifyImgContext.defaultImgUrl = options.defaultImgUrl

userContext.getUserPreference = options.getUserPreference
userContext.setUserPreference = options.setUserPreference
userContext.setUserLocale = options.setUserLocale

productIdentificationContext.getProductIdentificationPref = options.getProductIdentificationPref
productIdentificationContext.setProductIdentificationPref = options.setProductIdentificationPref
Expand Down
32 changes: 13 additions & 19 deletions src/store/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,26 @@ declare let process: any;
export const useUserStore = defineStore('user', {
state: () => {
return {
localeOptions: process.env.VUE_APP_LOCALES ? JSON.parse(process.env.VUE_APP_LOCALES) : { "en": "English" },
preference: {
locale: 'en'
} as any
localeOptions: process.env.VUE_APP_LOCALES ? JSON.parse(process.env.VUE_APP_LOCALES) : { "en-US": "English" },
locale: 'en-US'
}
},
getters: {
getLocale: (state) => state.preference.locale,
getLocale: (state) => state.locale,
getLocaleOptions: (state) => state.localeOptions
},
actions: {
setLocale(payload: string) {
// update locale in state and globally
i18n.global.locale.value = payload
this.setPreference({ locale: payload })
},
async setPreference(payload: any) {
this.preference = { ...this.preference, ...payload }
await userContext.setUserPreference({
'userPrefTypeId': 'LOCALE_PREFERENCE',
'userPrefValue': JSON.stringify(this.preference)
})
},
async getPreference(token: any, baseURL: string) {
async setLocale(locale: string) {
let matchingLocale = Object.keys(this.localeOptions).find((option: string) => option === locale)
// If exact locale is not found, try to match the first two characters i.e primary code
matchingLocale = matchingLocale || Object.keys(this.localeOptions).find((option: string) => option.slice(0, 2) === locale.slice(0, 2))
const newLocale = matchingLocale || this.locale

try {
this.preference = await userContext.getUserPreference(token, baseURL, 'LOCALE_PREFERENCE')
// update locale in state and globally
i18n.global.locale.value = newLocale
this.locale = newLocale
await userContext.setUserLocale({ newLocale })
} catch (error) {
console.error(error)
}
Expand Down

0 comments on commit d984c85

Please sign in to comment.