Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
1 Balas
2992 Tampilan

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
Buang
Jawaban Terbai
  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
Buang
Post Terkait Replies Tampilan Aktivitas
1
Mar 24
1595
2
Mar 24
1673
0
Nov 23
1279
2
Mar 24
6039
3
Mar 24
5079