flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
11.39k stars 446 forks source link

Feature request: Internationalization #103

Open mikaelho opened 2 years ago

mikaelho commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 9 months ago

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 commented 9 months ago

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 commented 6 months ago

https://github.com/flet-dev/examples/issues/132