amisadmin / fastapi-amis-admin

FastAPI-Amis-Admin is a high-performance, efficient and easily extensible FastAPI admin framework. Inspired by django-admin, and has as many powerful functions as django-admin.

Home Page:http://docs.amis.work

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dialog buttons and title keep showing Chinese caption

swelcker opened this issue · comments

fastapi-amis-admin>=0.2.3
fastapi_user_auth
fastapi-scheduler

All the UI (tables etc. is shown in en_US) but when i click on delete of a record or on pause in the scheduler the popup dialog show title and button captions in chinese.

What am i doing wrong?

Btw, this has nothing todo with the Scheduler, it is the same also for the CRUD Models/Tables delete actions etc.
Also checked it is the same with amis 1.10.2 or 2.0.2

Hello, I have read your question, which seems to be caused by the code of Baidu Amis. You can try to find out the relevant solution on the Amis official website. I'm really sorry that I haven't found the relevant solution yet. If you find it, please reply here. Thank you.

The problem is at the end very simple and i guess simple to solve (Spoiler: var locale in app.html needs to be 'en-US' instead of 'en_US'), the SDK.js is initialized in the app.html template:

    let amisInstance = amis.embed(
        '#root',
        app,
        {location: history.location, locale: "${locale}"},
        {

here the var {locale} is "en_US" instead of "en-US" and that is causing the trouble.

I tracked the error and found the following:

It starts with the init of fastapi_amis_admin/init.py
where the import of 'from .utils.translation import i18n' which imports 'utils/translation.py'
which creates an instance of 'i18n = I18N()'.

In the init of I18N the self_language gets initialzed with:
'self._language: str = self.set_language()'

so it calls set_language with language=None:

language = language or os.getenv("LANGUAGE") or os.getenv("LANG") or locale.getdefaultlocale()[0]

in my case those condition gets the following values:

   os.getenv("LANGUAGE") == '"en-US"'
   os.getenv("LANG") == 'de_DE.UTF-8'
   locale.getdefaultlocale()[0] == 'de_DE'

so the result in var language == '"en-US"'

AND NOW THE LINE WHO CAUSES THE ISSUE in set_language():

self._language = "zh_CN" if language.lower().startswith("zh") else "en_US"

now _language is "en_US" which is used and returned by function get_language()
which is used by file admin/admin.py in class PageAdmin in function async def page_parser:

   async def page_parser(self, request: Request, page: Page) -> Response:
          if request.method == "GET":
                 result = page.amis_html(
                 template_path=self.template_name,
                 locale=_.get_language(),

and here the 'locale' var is set which is used in the app.html script.

So even if you set it right in os.env LANGUAGE, the function set_language() kills it.
As amis supports de, en, zh your switch/if needs to be changed.
The bug is in fastapi_amis_admin and not in amis, amis docu shows everywhere that they use - instead of _

My workaround was to override 'async def page_parse, which works when a page is reloaded but the moment i chose another entry in the nav than it is back to zh language till the next reload, maybe that is more related to amis but i am not sure yet how the click on the nav works in terms of functions and events in fastapi_amis_admin, myabe someone else has an idea.

Ok, thank you very much for finding this problem, this problem is too hidden, thank you for your feedback.
Simple fix method is to replace the value of the locale parameter in the amis.components.Page.amis_html method, replace _ with -.
Next version will fix this problem.

Just figured that this is only half of the problem, the full fix is as you proposed to change amis.components.Page.amis_html

BUT you also need to change app.html, sepcificaly add the locale var to the updateProps call:

    history.listen(state => {
        amisInstance.updateProps({
            location: state.location || state,
            locale: "${locale}" // THIS IS THE ADDITIONAL NEEDED LINE
        });
    });

Becasue else the language is only correct when you make a full page refresh and amis_html was called, if only the dict was called then the language was back to default.

Thanks for your solution, I will fix it in the next version.

you are more then welcome, thx for the awsome work!

While you touch def amis_html() of component.Pageyou maybe can consider to change also the theme_css line to:

theme_css = f'<link href="{cdn}/{pkg}/sdk/{theme}.css" rel="stylesheet"/>' if theme else ""

this would allow us to use all availabe amis themes and not just antd and cxd, this way we would be able to use dark and ang too.

Have a nice weekend!

A very good idea. It has been added to V0.2.4 and released.

Thank you very much. I propose you may reconsider using a Literal for the language var in 'admin.Settings', it causes an error:

pydantic.error_wrappers.ValidationError: 1 validation error for Settings
language
  unexpected value; permitted: 'zh_CN', 'en_US', '' (type=value_error.const; given='en_US'; permitted=('zh_CN', 'en_US', ''))

Why did this error happen? I don't understand.

I just guess the problem with this Literal is the fact that it is a env var in the case of the language field, i wasn't bale to figure this out but that is the only difference i can see, compared to the theme literal, is the os.env relationship.

I was only able to make it work with inheriting admin.setting and redefine language: str = "".

The moment you allow str too, the error is gone:

language: Union[Literal["de_DE", "en_US", "zh_CN"], str] =  ""

So i am sorry but i wasn't able to find a solution for it. Just a workaround for myself.

I just guess the problem with this Literal is the fact that it is a env var in the case of the language field, i wasn't bale to figure this out but that is the only difference i can see, compared to the theme literal, is the os.env relationship.

I was only able to make it work with inheriting admin.setting and redefine language: str = "".

The moment you allow str too, the error is gone:

language: Union[Literal["de_DE", "en_US", "zh_CN"], str] =  ""

So i am sorry but i wasn't able to find a solution for it. Just a workaround for myself.

Ok, update in the next version, thanks