Translating Values to Many Languages in Tableau Desktop

As a Tableau user, I usually set the default language for Tableau Desktop as English. However, in some cases, if a user works for a global company, many users worldwide can use the dashboard. The language could be a barrier for the user to interact or get insights from the dashboard.

Thinking of how any users worldwide can interact with the dashboard easily and get insights into what the dashboard transfers, I applied Python to write the script to translate into many languages I like in Tableau Desktop. In this blog, I will share how you can switch languages with a parameter for the fields in Tableau Desktop including these steps:

1/ Set up environment
2/ Testing Python script in Jupyter Notebook
3/ Create a Language List Parameter
4/ Writing Python scrip in the Calculated Field

Are you ready? If so, let's get started!

Translate to many languages on Tableau Desktop

1/ Set up the Environment

In this blog, I use the deep-translator package. You can install that package by opening the Command Prompt on Windows (or Terminal on Mac) and typing:

pip install deep-translator

After a few minutes to install that package, I open Jupyter Notebook to test that package. You can read the documentation page of that package here.

You also need to install TabPy on the command prompt to run the Python script in Tableau Desktop. If you haven't installed it yet, I have a blog to show the instructions. You can read it here.


2/ Testing Python Script in Jupyter Notebook

In this step, you can use any IDEs that you usually use (e.g.: Python Charm, Visual Studio Code, in the terminal, Jupyter Notebook). I mostly use Jupyter Notebook, so use it as an example. But make sure the deep_translator is installed in the IDE you are working on.

In Jupyter Notebook, I import the Google Translator function from the deep_translator package that I just installed from the Command Prompt. (Fig. 1)

I also created a list called sub_category as same as the field in the SuperStore dataset in Tableau Desktop with some values: chair, table, laptop, phones, and copiers.

In the next line, I check how many languages that GoogleTranslator function can support by typing:

GoogleTranslator().get_supported_languages(as_dict=True)

I assigned that line to a variable called langs_dict. It will list all languages in a dictionary with the name of the language and the abbreviation of that language (mostly in 2 characters).

Fig.1: Import the GoogleTranslator function from deep_translator in Jupyter Notebook

Now, I want to test a sentence to translate it into Korean language. I will create a variable called text with a sentence: "Hello! My name is Le Luu". In another line, I apply the GoogleTranslator function to translate that sentence into Korean.

GoogleTranslator(source='auto', target='ko').translate(text=text)

And assigned it to the translated_text variable. I called that variable to print out the result. (Fig. 2)

Fig. 2: Apply Google Translator function to translate a sentence into a different language

Next, I would like to translate all the text in the list of sub_category. Assume that the list is the same as the field in the SuperStore dataset.

translated_list = []
def detect_translate(list_text,language):
    for text in list_text:
        translated_text= GoogleTranslator(source='auto',target=language).translate(text=text)
        translated_list.append(translated_text)
    return translated_list

I created an empty list called translated_list to store all the text after translation. Then, I create a function called detect_translate with 2 parameters list_text and language. Inside the function, I used the for loop to loop into each value in the list_text. I copied the translated_text line above into here, but my target now is the language parameter. So, if I change the language parameter, the result will return that language after translation. After that, I append the translated_text value into my translated_list. Finally, I return the full translated_list after translating all values in the list_text.

I called the function and put the sub_category argument in with the Vietnamese language (abbreviation is "vi"). The output is the list of the translated text in Vietnamese. (Fig. 3)

Fig. 3: Write detect_translate function with 2 parameters

Now, you are ready to open Tableau Desktop to try that function.


3/ Create a Language List Parameter

Before opening Tableau Desktop, let's open Command Prompt on Windows (or Terminal on Mac) to run TabPy. Then, check the connection with TabPy in Tableau Desktop.

Fig. 4: Connect to TabPy in Command Prompt on Windows

If the connection is successfully connected, let's open the SuperStore sample dataset in Tableau Desktop.

I created a new Parameter with the name Language List Param type String. (Fig. 5)
- In the Allowable values, I chose List.
- In the value column, I type all the Abbreviation languages that I want to translate. You can check the language code in Python (Fig. 1). Make sure that the language code you put in the Value column matches with the language code from GoogleTranslator.
- In the Display As column, I type the full text of that language.
- Click OK.

Fig. 5: Create a new parameter to list all languages that you want to translate

Now, let's create a calculated field with the Python script.


4/ Writing Python Script in the Calculated Field

I drag SUM(Sales) into Columns and Sub_Category into Rows. Then, sorting the Sub-Category by the SUM(Sales). (Fig. 6)

Fig. 6: Set up the view with Sub-Category and SUM(Sales)

After that, I created a new calculated field. I put a name for that field Deep_translator_sub_categ. I know that the result will return a String data type, so I use the SCRIPT_STR function. You can read more about those SCRIPT functions from my previous blog here.

Now, I only need to copy the code I have from Jupyter Notebook into here. Remember to import the deep_translator package with the GoogleTranslator function. (Fig. 7)

Fig. 7: Type Python script in the calculated field

Note that, in the end, I returned the function detect_translate(_arg1, _arg2[1]). It means I called the function that I defined above and put 2 arguments.

The first argument _arg1 which is the MAX([Sub-Category]) (need an aggregate function).

The second argument _arg2[1] which is the Language List Param. Note that, the argument is followed by an element [1]. It will let the Python script know that gets only 1 element from the Language List. The number doesn't matter. Whenever you switch the element in the parameter, it will update this _arg2.

Now, drag it next to the Sub-Category field in Rows. Show the parameter and also change the title to let you know the language you are choosing.

Fig. 8: Translating Sub-category into many languages by using Parameter

Congratulations!!! Now you can translate to any language you want. If you want more languages, you only need to edit the Parameter and add the language code you want.

Or you can create a new parameter to add more dimensions not only the Sub-Category field. It will dynamically switch the dimension field and the language.


Is it interesting? I hope this blog will be helpful to you if you are looking for a way to translate the field in Tableau Desktop. However, there are some cons in using this way.

First, you need to install TabPy to run it. You cannot publish a workbook using TabPy on Tableau Public or you also cannot publish on Server/ Cloud if not installing TabPy.

Second, this method could cause the performance of the dashboard to slow. In the example above, the text in the Sub-Category is not many. If you have a long text, it would take a long time to translate. In that case, you can consider using Filter to only show the text you want or create an alias, or group values.

Thank you for reading my blog! See you soon in the next blog!

Author:
Le Luu
Powered by The Information Lab
1st Floor, 25 Watling Street, London, EC4M 9BR
Subscribe
to our Newsletter
Get the lastest news about The Data School and application tips
Subscribe now
© 2024 The Information Lab