For this script to work, you'll first need to add a data table with the following structure:
This can be achieved through various methods like MS Excel, CSV, Access or SQL databases, but it must have a structure similar to this
LangId | Original | English | Espanol |
---|---|---|---|
1 | |||
2 | |||
3 |
The imported column types use the External Name of the column, so ensure that the matching name is in the "Original" column of the Language Data Table.
The calculated column types use the Description in Spotfire, so ensure that there is an index entry in the description in Spotfire and the matching entry in the "Original" column of the Language Data Table,
For document properties you want translated, ensure that the document property name contains "Translation" and that an index entry is in the description field. This will get compared to the "Original" column of the Language Data Table for translation.
When you have added the data table to Spotfire, name it "LanguageDefinitions".
In the data table properties, untick "Show in user interface". This ensures that the table remains invisible and independent of any other tables
In the relations tab, you can also change the colour of the relation colour to ensure there's a difference between your other data tables.
The following code is used for querying and obtaining the unique ids of pages and visualisations. These are required for the change language script to work successfully.
# Declarations and initial system setup from Spotfire.Dxp.Application.Visuals import * #---------------------------------------------------------------------- for p in Document.Pages: print p.Title + " > " + str(p.Id) for v in p.Visuals: print " " + v.Title + " > " + str(v.Id) |
Add the following code into your document with the following parameter (Lang):
# Declarations and initial system setup from Spotfire.Dxp.Data import * from Spotfire.Dxp.Application.Visuals import * from System.Collections.Generic import List from System import Guid pguid=[] vguid=[] plangid=[] vlangid=[] datatables=[] languagelist=[] dt = Document.Data.Tables["LanguageDefinitions"] #---------------------------------------------------------------------- # Build an array of page titles to translate # To add a language translation for page titles, add a group of # variables like below to build an array of guids and langids # pguid.append("35def6ee-5edd-44dc-a4dd-c73d99dbb945") # plangid.append("1") #---------------------------------------------------------------------- # Build an array of visualisation titles in this section # To add a language translation for visualisation titles, add a group # of variables like below to build an array of guids and langids # vguid.append("35def6ee-5edd-44dc-a4dd-c73d99dbb945") # vlangid.append("2") #Page # --------------------------------------------------------------------- # Build an array of data tables in this section to translate # To add a language translation for columns, add a variable # like below to build an array of data tables to translate # datatables.append("Data Table Name") #---------------------------------------------------------------------- # Build an array of languages available in your data table for c in dt.Columns: if str(c)<>'LangId' and str(c)<>'Original': languagelist.append(str(c)) #---------------------------------------------------------------------- # Set Default Document Properties #if Lang == "English": #elif Lang == "Espanol": #---------------------------------------------------------------------- # Cycle through the list and change language on the page titles for la in languagelist: if str(la) == Lang: for p in pguid: MyPage=Document.Pages.TryGetPage(Guid(p)) RowSelection = dt.Select('LangID='+plangid[pguid.index(p)]) cursor = DataValueCursor.CreateFormatted(dt.Columns[str(la)]) listofValues=List[str]() for row in dt.GetRows(RowSelection.AsIndexSet(),cursor): rowIndex=row.Index value1=cursor.CurrentValue listofValues.Add(value1) for val in listofValues: MyPage[1].Title=val # --------------------------------------------------------------------- # Cycle through all specified visualisations and change the title for la in languagelist: if str(la) == Lang: for p in pguid: MyPage=Document.Pages.TryGetPage(Guid(p)) for i in vguid: MyVis=MyPage[1].Visuals.TryGetVisual(Guid(i))[1] if MyVis: RowSelection = dt.Select('LangID='+vlangid[vguid.index(i)]) cursor = DataValueCursor.CreateFormatted(dt.Columns[str(la)]) listofValues=List[str]() for row in dt.GetRows(RowSelection.AsIndexSet(),cursor): rowIndex=row.Index value1=cursor.CurrentValue listofValues.Add(value1) for val in listofValues: MyVis.Title = val # --------------------------------------------------------------------- # Cycle through the specified data tables and translate column names by # comparing original to external name. For calculated columns, place the # original/index item in the description field. for la in languagelist: if str(la) == Lang: for t in datatables: AvE=Document.Data.Tables[t] for col in AvE.Columns: RowSelection = dt.Select('Original="'+str(col.Properties.ExternalName)+'"') if sum(RowSelection.AsIndexSet()) == 0 and col.Properties.Description <> '': RowSelection = dt.Select('Original="'+str(col.Properties.Description)+'"') cursor = DataValueCursor.CreateFormatted(dt.Columns[str(la)]) listofValues=List[str]() for row in dt.GetRows(RowSelection.AsIndexSet(),cursor): rowIndex=row.Index value1=cursor.CurrentValue listofValues.Add(value1) for val in listofValues: col.Properties.Name=val #--------------------------------------------------------------------- # Find and set the document property translations for custom # expressions. Anything with the prefix "Translation" will be # translated with the original/index item in the description. for la in languagelist: if str(la) == Lang: for prop in Document.Data.Properties.GetProperties(DataPropertyClass.Document): if prop.IsUserVisible and prop.IsUserEditable and str(prop.Name).find('Translation')>=0: RowSelection = dt.Select('Original="'+str(prop.Description)+'"') cursor = DataValueCursor.CreateFormatted(dt.Columns[str(la)]) listofValues=List[str]() for row in dt.GetRows(RowSelection.AsIndexSet(),cursor): rowIndex=row.Index value1=cursor.CurrentValue listofValues.Add(value1) for val in listofValues: prop.Value=val #---------------------------------------------------------------------- |
Enter the necessary information into the IronPython sections.
For pages, the following section is required:
#---------------------------------------------------------------------- # Build an array of page titles to translate # To add a language translation for page titles, add a group of # variables like below to build an array of guids and langids pguid.append("35def6ee-5edd-44dc-a4dd-c73d99dbb945") plangid.append("1") #---------------------------------------------------------------------- |
The plangid refers to the unique identification (LangId) on the language data table.
Follow a similar process for the visualisation section:
#---------------------------------------------------------------------- # Build an array of visualisation titles in this section # To add a language translation for visualisation titles, add a group # of variables like below to build an array of guids and langids # Page vguid.append("35def6ee-5edd-44dc-a4dd-c73d99dbb945") vlangid.append("2") # --------------------------------------------------------------------- |
The vlangid refers to the unique identification (LangId) on the language data table.
For the data tables, simply append a list of the data tables in which the imported columns will get compared and translated. The id does not matter in this case. The script mainly looks at the original column on the language data table and compares that against the ExternalName in the Spotfire tables.
# --------------------------------------------------------------------- # Build an array of data tables in this section to translate # To add a language translation for columns, add a variable # like below to build an array of data tables to translate datatables.append("Data Table Name") #---------------------------------------------------------------------- |
Lastly, set any default document properties in the last section
#---------------------------------------------------------------------- # Set Default Document Properties if Lang == "English": Document.Properties["InitialSetting"] = "Hello" elif Lang == "Espanol": Document.Properties["InitialSetting"] = "Hola" #---------------------------------------------------------------------- |
Finally, to enable the user to change the language as required, add a dropdown list to a text-box visualisation on a Spotfire dashboard.
Select "new" to add a document property, this will be the setting that determines the language.
Input the following details and click "OK":
The document property has been added, now click "Script"
Select "Execute the script selected below", select the relevant "Language" script, select the "Property" and click "Select Property"
Select the "Language" property and click "OK"
Click "OK"
Back to the main screen, select "Fixed values" and input the languages that you have translations for.
When finished, click "OK"
Click the save icon on the text-box to save the dropdown:
The dropdown will now appear and when you change the setting, the dashboard will change language