Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
2791 Visualizzazioni

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
Abbandona
Risposta migliore
  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
Abbandona
Post correlati Risposte Visualizzazioni Attività
1
mar 24
1473
2
mar 24
1502
0
nov 23
1150
2
mar 24
5765
3
mar 24
4877