How to create an internationalized Google App Engine application

前端 未结 2 978
陌清茗
陌清茗 2020-12-13 21:41

I would like to provide my Python GAE website in the user\'s own language, using only the tools available directly in App Engine. For that, I would like to use GNU gettext

相关标签:
2条回答
  • 2020-12-13 22:17

    You can use the Django internationalisation tool, like explained here.

    They are also saying that there is no easy way to do this.

    I hope that helps you :)

    0 讨论(0)
  • 2020-12-13 22:27

    As my needs were simple, I used a simple hack instead of (unavailable) gettext. I created a file with string translations, translate.py. Approximately like this:

    en={}
    ru={}
    
    en['default_site_title']=u"Site title in English"
    ru['default_site_title']=u"Название сайта по-русски"
    

    Then in the main code I defined a function which returns a dictionary with translations into the most suitable language from the list (the first one to have a translation is used or English):

    import translate
    
    def get_messages(languages=[]):
        msgs=translate.en
        for lang in languages:
            if hasattr(translate,lang):
                msgs=getattr(translate,lang)
                break
        return msgs
    

    Usage:

    msgs = get_messages(["it","ru","en"])
    hi = msgs['hello_message'] % 'yourname'
    

    I also defined a helper function which extracts a list of languages from Accept-Language header.

    It's not the most flexible solution, but it doesn't have any external dependencies and works for me (in a toy project). I think translate.py may be generated automatically from gettext files.

    In case you want to see more, my actual source is here.

    0 讨论(0)
提交回复
热议问题