This question has been flagged


In my Odoo 17 application, I have enabled the Arabic language in the backend. When I access the Odoo website and change the language to Arabic, the error messages are displayed correctly in this language.


However, I have developed a custom API and I want the error messages to change language based on the language chosen by the user. I have created a .po file with the translations and I have created this API to change the language:



@http.route('/api/lang/', type='http', auth="public",csrf=False, cors='*')
def change_lang(self, lang):
    """ :param lang: supposed to be value of `url_code` field """
    try:
        lang_code = request.env['res.lang']._lang_get_code(lang)
        # replace context with correct lang, to avoid that the url_for of request.redirect remove the
        # default lang in case we switch from /fr -> /en with /en as default lang.
        request.update_context(lang=lang_code)
        request.env.user.lang = lang_code
        request.future_response.set_cookie('frontend_lang', lang_code)
        return request.make_response(json.dumps({'success': True,'lang_code':lang_code, 'contxt':  request.env.context, 'env lang': request.env.lang, 'public user': request.env.user.lang}),
                              headers={'Content-Type': 'application/json'})
    except Exception as e:
        return {'success': False, 'error': str(e)}



When I call the URL `/api/lang/ar`, I receive the following response:



{
    "success": true,
    "lang_code": "ar_001",
    "contxt": {
        "lang": "ar_001"
    },
    "env lang": "ar_001",
    "public user": "ar_001"
}



Then, when I call the API that returns an error message, for example `/ar/api/login` or `/api/login`, I receive the following response:



{
    "jsonrpc": "2.0",
    "id": null,
    "result": {
        "success": false,
        "error": "Access denied"
    }
}


The problem is that the error message "Access denied" is not displayed in the selected language (Arabic in this case), but remains in English. [citation:1][citation:2][citation:3][citation:4][citation:5][citation:6][citation:7]


However, if I change the language of my admin account (Mitchell Admin), the error message is then displayed in the selected language. Note that I am not logged in at all, I am accessing as a public user. [citation:8][citation:9][citation:10]


How can I make the error message display in the selected language, even when I am a public user?

Avatar
Discard