Download the Solution Example: 

  • Solution Name: Localization Example
  • Software Version: v10
  • Keywords: Localization, CultureInfo
  

Summary

This solution example demonstrates how to change the language of components using Localization options.


Technical Information

In this example, you will learn how to create your own Localization Tables and localize components through Draw mode and CodeBehind. You will also receive a few suggestions on how to manage your localization code.

To localize text, create a Localization Table. Go to Displays → Localization, create a new Table, give it a name and fill its OriginalText and TranslatedText columns.


In this example, we created two Localization Tables. You can either change the current localization by assigning the name of your Localization Table to Client.Localization, or by passing it as an argument to Client.SetLocalization. To return your solution to the default language, pass an empty string to the property or method.

To localize labels, use the Client.Locale method, and pass the text you want to translate as an argument. Certain components require that you set up the solution’s Culture Info to match the desired localization. Change the Client.CultureInfo property to one of the supported culture codes to do it. In this example, the DatePicker component spawns a calendar that relies on this variable to translate months and days of the week.

To manage Localization and CultureInfo, this solution relies on the languages dictionary:

Dictionary<string, (string localization, string cultureInfo)> languages;

public void StartLanguages()
{
	languages = new Dictionary<string, (string localization, string cultureInfo)>();
	languages.Add("English", ("", "en-US"));
	languages.Add("Português", ("Português", "pt-BR"));
	languages.Add("Español", ("Español", "es-ES"));
}

With this dictionary, changing the Localization through CodeBehind is simple. Whenever the localization is changed, we call the following method:

public void LocalizeAll(string language)
{
	@Client.Localization = languages[language].localization;
	@Client.CultureInfo = languages[language].cultureInfo;
	LocalizeEmployeeDataGrid();
	LocalizePositionComboBox();
}


CodeBehind is also needed to translate the ComboBox and the DataGrid. To translate the DataGrid, we first store its column titles in a list using StartEmployeeDataGrid, and translate them as needed using LocalizeEmployeeDataGrid:

TDataGrid employeeDataGrid = null;
List<string> employeeDataGridTitles;

public void StartEmployeeDataGrid()
{
	employeeDataGridTitles = new List<string>();
	employeeDataGrid = CurrentDisplay.GetControl("EmployeeDataGrid") as TDataGrid;
	foreach(var column in employeeDataGrid.Columns)
	{
		employeeDataGridTitles.Add(column.Title);
	}
} 

public void LocalizeEmployeeDataGrid()
{
	for(int i = 0; i < employeeDataGridTitles.Count; i++)
	{
		employeeDataGrid.Columns[i].Title = @Client.Locale(employeeDataGridTitles[i]);
	}
}


For the ComboBox indicating employee positions, we instead save the positions to a text array tag. To localize the ComboBox, we clear it and then repopulate it with the translated items.

TComboBox positionComboBox = null;

public void StartPositionComboBox()
{
	@Tag.Positions[0].Value = "Electrician";
	@Tag.Positions[1].Value = "Engineer";
	@Tag.Positions[2].Value = "Human Resources";
	@Tag.Positions[3].Value = "Software Developer";
	@Tag.Positions[4].Value = "Administrator";
	positionComboBox = CurrentDisplay.GetControl("PositionComboBox") as TComboBox;
	for(int i = 0; i < @Tag.Positions.ArraySize; i++)
		positionComboBox.Items.Add(@Client.Locale(@Tag.Positions[i].Value));
	@Tag.PositionIndex = 0;
}

public void LocalizePositionComboBox()
{
	// Empties the ComboBox and repopulates it with the localized versions of positions
	int index = @Tag.PositionIndex;
	positionComboBox.Items.Clear();
	for(int i = 0; i < @Tag.Positions.ArraySize; i++)
		positionComboBox.Items.Add(@Client.Locale(@Tag.Positions[i].Value));
	@Tag.PositionIndex = index;
}

In this solution example, we save the positions to a tag in order to insert them into the employee table using a SQL query called AddEmployee, which you can find in Datasets → Queries. With the way we set up our code, the ComboBox shows localized text, but new rows are inserted in accordance with the table’s original language.


Reference Information

→ See Displays Localization for more information on Localization options.

→ See ComboBox From Table Example for a simple guide on how to populate DataGrids and ComboBoxes.


In this section:

  • No labels