Skip to Content
Menú
This question has been flagged
1 Respondre
2653 Vistes

I am getting error when changing language,

Specifically, I use a module used to create a button to change the language.

when i change everything is ok except the Menu it is not changed until i f5 the page again.

I downloaded that module here: https://apps.odoo.com/apps/modules/16.0/web_systray_language/

This is what Js wrote:

/** @odoo-module **/
import {Dropdownfrom'@web/core/dropdown/dropdown';

import {DropdownItem} from'@web/core/dropdown/dropdown_item';

import {useService} from'@web/core/utils/hooks';

import {registry} from'@web/core/registry';

import {Component, onWillStart} from'@odoo/owl';

import {session} from'@web/session';

import {browser} from'@web/core/browser/browser';
export class SwitchLanguageMenu extends Component {

setup() {

this.langService = useService('lang');

this.ormService = useService('orm');
this.userLangCode = session.bundle_params.lang;


onWillStart(async () => {

this.langs = awaitthis.langService.getLangs();

this.lang = this.langs.find(l=>l.code === this.userLangCode);

 }

)

}


async switch(lang) {

if (this.lang === lang) {

return

}

await this.ormService.write('res.users', [session.uid], {

lang:lang.code,

});

browser.thread.refresh() }}


SwitchLanguageMenu.template = 'web_systray_language.SwitchLanguageMenu';

SwitchLanguageMenu.components = {Dropdown, DropdownItem};

export const languageSystrayItem = {

Component:SwitchLanguageMenu,

};

registry.category('systray').add('SwitchLanguageMenu', languageSystrayItem, {sequence:1});

Avatar
Descartar
Best Answer
  1. Ensure that the language change is successfully being saved in the user's profile. You can check this by verifying the lang field in the res.users model for the user.

  2. Add a custom function to handle the language change and force Odoo to reload the session using JavaScript.

Here's a modified version of your JavaScript code that includes a function to reload the session after changing the language:

/** @odoo-module **/

import { Dropdown } from '@web/core/dropdown/dropdown';

import { DropdownItem } from '@web/core/dropdown/dropdown_item';

import { useService } from '@web/core/utils/hooks';

import { registry } from '@web/core/registry';

import { Component, onWillStart } from '@odoo/owl';

import { session } from '@web/session';

import { browser } from '@web/core/browser/browser';


export class SwitchLanguageMenu extends Component {

setup() {

this.langService = useService('lang');

this.ormService = useService('orm');

this.userLangCode = session.bundle_params.lang;


onWillStart(async () => {

this.langs = await this.langService.getLangs();

this.lang = this.langs.find((l) => l.code === this.userLangCode);

});

}


async switch(lang) {

if (this.lang === lang) {

return;

}


await this.ormService.write('res.users', [session.uid], {

lang: lang.code,

});


// Clear the language cache to force Odoo to apply the language change

session.lang = lang.code;

session.user_context.lang = lang.code;

browser.session.session_reload();


// Use the following if you want to refresh the whole page instead of just the session

// window.location.reload();

}

}


SwitchLanguageMenu.template



Avatar
Descartar
Related Posts Respostes Vistes Activitat
1
de març 24
1395
2
de març 24
1391
0
de nov. 23
1062
2
de març 24
5623
3
de març 24
4718