Mulilingual Advice

How would you recommend I go about making my Anvil application multilingual?

There are things like this : https://github.com/tuvistavie/python-i18n
but I’d be curious if anyone has actual experience of doing something like this and could recommend something.

I have done it before in PHP, but I just created translation files that overwrote array elements (eg I would include default language file of English and override the arrays with the required language :

# In English.php - 
translate['welcome']="Welcome"

# In French.php - 
translate['welcome']="Bonjour"

Very hacky, I know!

Hi David,

That’s as good a way as any to start off! I would recommend using data bindings for this. Eg if you did:

# ... in some module somewhere:
TRANSLATIONS_FR = {'welcome': 'Bonjour'}
TRANSLATIONS_DE = {'welcome': 'Wilkommen'}
TRANSLATIONS_EN = {'welcome', 'Hi there'}
# ... and in the __init__ method for your form:
self.translations = TranslationModule.TRANSLATIONS_EN

And then you could data-bind the text property of your label to self.translations['welcome'].

(Extra suggestion, slightly dirty: Anvil silently swallows KeyErrors in data binding assignments, so that it can initialise forms when, eg, self.item is an empty dictionary. This means that you could actually do:
TRANSLATIONS_EN = {}, and then set all your default text properties in the designer to the English versions. That way, you can use the English in the designer, and get the translations at run-time.)


Is your Anvil app multi-lingual? Would you like to commission development of a powerful built-in internationalisation mechanism for Anvil apps? Contact support@anvil.works and ask about support and commissioning agreements! The ability to prioritise feature development is a valuable one, and it’s available exclusively to our support customers.

3 Likes

Hi,

and how do I translate for example default login form, confirmation emails etc.

Is it possible ?

Jan

Think you would need to use custom forms for that (see the custom login form article).

@david.wylie is referring to this cookbook example:

https://anvil.works/blog/custom-user-auth

Anvil has the anvil.users.login_with_form() method, but it also has lower-level functions such as anvil.users.login_with_email(). You can use these to build your own login flow.

The section of the refrence docs covering the Users Service Python API might also be helpful here.

In the cookbook example I’ve linked to above, we’ve already done it for you. You just need to add the Custom Signup Flow app as a dependency:

58

50

then create Signup and Login buttons somewhere:

46

then import the Custom Signup Flow app and call the login and signup methods (remember to bind the click methods to the Buttons and the show method to the Form):

import custom_signup.login_flow

# ...

  def button_login_click(self, **event_args):
    custom_signup.login_flow.login_with_form()

  def button_signup_click(self, **event_args):
    custom_signup.login_flow.signup_with_form()

  def form_show(self, **event_args):
    custom_signup.login_flow.do_email_confirm_or_reset()

Here’s an app that does that:

https://anvil.works/build#clone:HXPYK5NTCDSS6HU5=DVUUS3LQU3YOQ2YX47S3ES3X

If you want to modify the login and signup forms, for example to translate them, you can modify them in your copy of the Custom Signup Flow app. Remember to publish your changes if you are depending on the published version!

To do the translation, you might find this localisation app useful: https://anvil.works/blog/library-localisation

image

1 Like

(sorry completely forgot to link to it!)

1 Like

Bringing back an old conversation with one little question.
I need all of my UI to be fully Hebrew (RTL language).
Should the same solution work here too? Just using a big language dict that translate English for example to Hebrew (with this too of course)?
As in:
TRANSLATIONS_IL = {'welcome', 'ברוך הבא'}

Thanks.

Bringing this back again after a long time…

Is there any update to the best practices for this now, or is the advice from meredydd and shaun still the best way to do it?

If you do some forum searches, others have come up with different translation libraries over the years. This was the most recent one: Fluent for Anvil - Perfect Translations for your Anvil App

2 Likes