Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with Language settings #1045

Open
moovida opened this issue Feb 13, 2025 · 13 comments
Open

Problems with Language settings #1045

moovida opened this issue Feb 13, 2025 · 13 comments

Comments

@moovida
Copy link
Collaborator

moovida commented Feb 13, 2025

Let me open an issue about the problems with language settings, so that they can be sorted out in future.

Forcing a default language doesn't seem to be possible at the moment. Let's assume I want to load German by default.

I tried setting:

LANGUAGE_CODE = 'de'

but nothing changes.

One option, where applicable, is to remove all other languages, so that would lead to a setting like:

LANGUAGES = (
    ('de', 'Deutsch'),
)

This actually breaks the server:

g3w-suite-1  |   File "/usr/local/lib/python3.12/dist-packages/modeltranslation/admin.py", line 8, in <module>
g3w-suite-1  |     from modeltranslation import settings as mt_settings
g3w-suite-1  |   File "/usr/local/lib/python3.12/dist-packages/modeltranslation/settings.py", line 16, in <module>
g3w-suite-1  |     raise ImproperlyConfigured("MODELTRANSLATION_DEFAULT_LANGUAGE not in LANGUAGES setting.")
g3w-suite-1  | django.core.exceptions.ImproperlyConfigured: MODELTRANSLATION_DEFAULT_LANGUAGE not in LANGUAGES setting.

probably because the MODELTRANSLATION_DEFAULT_LANGUAGE is set to 'en' by default. In fact adding:

MODELTRANSLATION_DEFAULT_LANGUAGE = 'de'

makes things work. It actually also properly removes the languages combobox, which makes sense, but with the new nice UI, this makes the admin menu fall out of the browser window:

Image

@moovida
Copy link
Collaborator Author

moovida commented Feb 13, 2025

Just as an update. I created a minimalistic multilingual django project to test the settings and check if it is a suite interference that is disturbing the behavior. Fact is that django behaves exactly the same as the suite does. I have not been able to force a German session from an 'English browser'.

I attach the project if someone more proficient in django wants to give it a try without the hassle of creating it.

multilingual_project.zip

@Raruto
Copy link
Contributor

Raruto commented Feb 13, 2025

Hi @moovida,

🙏 preferably, when you want to report something, use one of provided issue template instead of the "blank one".

Image

Additionaly, when in doubt, create multiple issues rather than a single one that describes multiple problems (otherwise over time it will be difficult keep track of what has been solved).


the languages combobox fall out of the browser window
Image

Add this within your custom css:

/* HOTFIX FOR: https://github.com/g3w-suite/g3w-admin/issues/1045 */
.nav-links > li:last-child > .dropdown-menu {
  right: 0;
  left: auto;
}

@moovida
Copy link
Collaborator Author

moovida commented Feb 13, 2025

Hi @Raruto , sorry for the blank template, has been a miss of my side.

Regarding the multiple issues, I find these problems to be all part of the same.

Regarding the custom css, thanks, but I assume there a possible generic solution without custom.css?

@Raruto
Copy link
Contributor

Raruto commented Feb 13, 2025

Is there a generic solution without custom.css?

Currently ([email protected]), this is a edge case, as you first need to know how to manually edit the settings_docker.py file to reproduce it:

Image

For a "no code" solution, the "admin user" should first be able to choose the languages that wants ​​to activate directly from the dashboard:

image


Furthermore, as your application grows, you may want to add more links to the navigation menu (or change the default order).

Through custom css/js you can achieve this quickly and without waiting for a new release.

Image

That said, keep in mind that this are still one-time configs.

Everything can be improved, any further help in coding it is always welcome.

@moovida
Copy link
Collaborator Author

moovida commented Feb 13, 2025

The reason I am actually asking this if because I am trying to widen the branding documentation page I set up: https://github.com/g3w-suite/g3w-suite-documentation/blob/dev/branding.md

I wanted to add the language part to make it more complete, but it seems that setting a default language is not possible (at least I have not been able even in the plain django project, which would be nice if someone could check @wlorenzetti ) and setting just one language has bit of a hassle.

It is fine to me to also leave that part out and take it as currently not feasible.

@Raruto
Copy link
Contributor

Raruto commented Feb 13, 2025

@moovida This is what I see locally:

# settings_docker.py

LANGUAGE_CODE = 'it'
LANGUAGES= (
    ('it', 'Italiano'),
)
MODELTRANSLATION_DEFAULT_LANGUAGE = 'it'

Image

It's a custom branch * I'm working on, but as you can see the language selector doesn't appear.

* ie. UI is slightly different (and not yet synced with: #1041)

@moovida
Copy link
Collaborator Author

moovida commented Feb 13, 2025

Hi @Raruto , I am not sure if I am not seeing something. As I mentioned in the first issue, this is what happens also to me.

Setting:

LANGUAGE_CODE = 'it'
LANGUAGES= (
    ('it', 'Italiano'),
)
MODELTRANSLATION_DEFAULT_LANGUAGE = 'it'

does work properly (apart of the menu glitch).

But the important thing discussed previously with Walter, was if there is a possibility to force a language using the settings.
I.e. being able to force Italian as default language, even if the browser is localized differently.
But this use case should allow for the other languages to be available. As far as I have seen, it is not possible (and maybe also in plain django). I was looking for extra knoledge input from django experts on this though.

@Raruto
Copy link
Contributor

Raruto commented Feb 14, 2025

@moovida Try something like this (it can be improved, not the best place to make changes..):

config/g3w-suite/settings_docker.py

LANGUAGE_CODE = 'it'

# LANGUAGES= (
#     ('it', 'Italiano'),
# )

# MODELTRANSLATION_DEFAULT_LANGUAGE = 'it'

g3w-admin/base/settings/__init__.py

...

MIDDLEWARE =  ['core.i18n_middleware.i18n_middleware'] + MIDDLEWARE + G3WADMIN_MIDDLEWARE

...

g3w-admin/core/i18n_middleware.py

# Based on: https://gist.github.com/vstoykov/1366794?permalink_comment_id=3649179#gistcomment-3649179

from contextlib import suppress

from django.conf import settings

def i18n_middleware(get_response):
    """
    Ignore Accept-Language HTTP headers.

    This will force the I18N machinery to always choose settings.LANGUAGE_CODE
    as the default initial language unless another one is set via sessions or cookies.

    Should be installed *before* any middleware that checks
    request.META['HTTP_ACCEPT_LANGUAGE'], namely
    `django.middleware.locale.LocaleMiddleware`.
    """

    def middleware(request):
        lang = getattr(settings, 'LANGUAGE_CODE')
        accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '').split(',')

        with suppress(ValueError):
            # Remove `lang` from the HTTP_ACCEPT_LANGUAGE to avoid duplicates
            accept.remove(lang)

        accept = [lang] + accept
        request.META['HTTP_ACCEPT_LANGUAGE'] = f"""{','.join(accept)}"""
        return get_response(request)

    return middleware

@moovida
Copy link
Collaborator Author

moovida commented Feb 14, 2025

Uhhh, nice one @Raruto

This works well.

I could add this as a section to the branding page.

Do you think a proposal could be made to add a LANGUAGE_FORCE_CODE to the settings that would then enable the use of i18n_middleware:

langForce = getattr(settings, 'LANGUAGE_FORCE_CODE')
if langForce:
   MIDDLEWARE = ['core.i18n_middleware.i18n_middleware'] + MIDDLEWARE + G3WADMIN_MIDDLEWARE
else:
   MIDDLEWARE = MIDDLEWARE + G3WADMIN_MIDDLEWARE

and then use the LANGUAGE_FORCE_CODE later in the i18n_middleware.

I am honestly not sure what can be done inside an init file.

@Raruto
Copy link
Contributor

Raruto commented Feb 14, 2025

IMHO, for these kind customizations (ie. on production sites) would be better to first try to write an own python plugin (like WordPress' child themes) and keep the necessary changes in there. *

Every time a new version is released, for a casual user, the settings_docker.py (and the entire docker repository) can become quite difficult to keep aligned (ie. think about upgrading v3.9xv4.x).


Do you think a proposal could be made to add a LANGUAGE_FORCE_CODE to the settings that would then enable the use of i18n_middleware:

I don't know if it's that painless (it should be tested with other apps too), better if sponsored.


* here you can see an example app that test/add a your own custom MIDDLEWARE:

You can also reason in a similar way for changing other settings (LANGUAGE_CODE, LANGUAGES, ..).

For more info:

@moovida
Copy link
Collaborator Author

moovida commented Feb 14, 2025

Hm, that sounds actually like a clean way to go. Can you elaborate on that?
You mean a plugin that would override the language setting, implement and finally add the middleware?

@moovida
Copy link
Collaborator Author

moovida commented Feb 14, 2025

Sorry @Raruto , tried to edit the above comment since it started too early, but was not allowed to submit the changes.

I will dig into the portal plugin to see if that answeres all my questions. Thank you

@Raruto
Copy link
Contributor

Raruto commented Feb 14, 2025

Hm, that sounds actually like a clean way to go. Can you elaborate on that?

You can also reason in a similar way for changing other settings (LANGUAGE_CODE, LANGUAGES, ..).

Yes, there are many possibilities to create a plugin:

  • pip+git ← explained within portal's README
  • git only ← explained within portal's README
  • docker binding ← for nerds..
  • js / css only ← I don't think it's ever been implemented properly (within admin / client), but ideally you just need add a string to settings_docker.py (ref: PORTAL_CUSTOM_JS / PORTAL_CUSTOM_CSS)

You can take also look at the following: g3w-admin-ps-timeseries to understand how a Django plugin works.

It's the simplest one I can think of (ie. which does not require compiling JS code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants