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

Feature request: Internationalization #103

Open
mikaelho opened this issue Jul 26, 2022 · 5 comments
Open

Feature request: Internationalization #103

mikaelho opened this issue Jul 26, 2022 · 5 comments
Labels
enhancement Improvement/Optimization feature request Suggestion/Request for additional feature

Comments

@mikaelho
Copy link
Contributor

Support showing UI in user's language.

This is not necessarily a "flet concern", as all the necessary machinery can be "on top" of the controls (e.g. Text control value is given by a function call that gets user's language and a message identifier as parameters, and returns the actual text to show in user's language).

But, it would be nice to have a shared/recommended way to implement this, instead of everyone rolling their own.

Python standard library provides gettext, which is used by heavyweights like Django.

We could even support automatic (and risky) batch translation to various languages, see e.g. this article.

@FeodorFitsner
Copy link
Contributor

FeodorFitsner commented Jul 26, 2022

I went through https://docs.flutter.dev/development/accessibility-and-localization/internationalization and it's a kind of scary how it's made on Flutter side.

I'm thinking about approach on Python (app) side instead. Also, let's not forget that it's not only text-like property should be able to localize, but often control dimensions could be localized too to accomodate longer text or image src to include a different graphic.

@LaoshuBaby
Copy link

Also, let's not forget that it's not only text-like property should be able to localize, but often control dimensions could be localized too to accomodate longer text or image src to include a different graphic.

It's more than that

Some languages are RTL, so the entire UI must be completely mirrored left and right, for example: Arabic Wikipedia or Openstreetmap in Hebrew

Even font used can make things, CJK character share same unicode code but the should be different glyph, you need to detect using which set of font for different east asian language. What more frustrated its their fallback chain.

troublesomeCJK

@bambier
Copy link

bambier commented Jan 26, 2024

I had created this app repo. I tried to translate the whole UI with translation files created by the py-auto-translate tool, which generates .po/.mo files. This is a platform-independent implementation, same as in Django.

There is no problem with direction change, characters, etc, but it seems the texts are stored somewhere, perhaps in a cache, and are not being updated even after page.update() call.

@bambier
Copy link

bambier commented Feb 10, 2024

I had created this app repo. I tried to translate the whole UI with translation files created by the py-auto-translate tool, which generates .po/.mo files. This is a platform-independent implementation, same as in Django.

There is no problem with direction change, characters, etc, but it seems the texts are stored somewhere, perhaps in a cache, and are not being updated even after page.update() call.

Now I found tricky way to internationalize application (for desktop and mobile only) with this code you can generate and use gettext-based translated texts also with py-auto-translate generate .po/.mo files.
For changing language you have to use structure like this:

import os
import sys
from translation import set_current_language

import flet as ft

# Codes


class BaseView(ft.View):
    """Base View class for all views in application
    """
    page: ft.Page

    def __init__(
            self,
            page: ft.Page,
            *args,
            **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.padding = 0
        self.expand = True
        self.page = page
        self.page.on_resize = self.on_resize

        self.set_content()

    def on_resize(self, event: ft.ControlEvent, *args, **kwargs):
        self.page = event.page
        self.set_content()
        self.page.update()

    def chlang(self, lang: str = "en", *args, **kwargs) -> None:
        """Change language to given language if language isn't provided it switchs to oposit language

        Args:
            lang (str, optional): language code. Defaults to English (en).
        """
        set_current_language(page=self.page, lang=lang)
        self.set_content()

    def set_content(self):
        pass

Now when you press button to change language after changing it, it calls set_content in that method you have to set your content. I mean, instead of setting contents in __init__ method, set all contents in this method and just call it when ever you want to renew everything.

I'm not sure about Chinese/Korean/Japanese, but it works for languages such as English and RTL languages with Persian based glyph such as Arabic, Pashto, etc.

You can work around code with locale library in python for languages you that don't know about it's direction or type or name.

@BrentHuang
Copy link

flet-dev/examples#132

@ndonkoHenri ndonkoHenri changed the title Feature idea: Provide a "standard flet way" to translate UI to user language Feature request: Internationalization Jul 7, 2024
@ndonkoHenri ndonkoHenri added enhancement Improvement/Optimization feature request Suggestion/Request for additional feature labels Jul 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement/Optimization feature request Suggestion/Request for additional feature
Projects
None yet
Development

No branches or pull requests

6 participants