Localization in .NET MAUI DataForm (SfDataForm)
21 Jan 20256 minutes to read
Localization is translating the application resources into different languages for specific cultures. The SfDataForm can be localized by adding a resource file.
Localize the DataFormViewItem DisplayAttribute values and ValidationAttribute values by using the ResourceType property of the attribute or using the GenerateDataFormItem event when auto-generate true.
Setting CurrentUICulture to the application
Application culture can be changed by setting the CurrentUICulture in the App.xaml.cs file.
using System.Globalization;
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new AppShell();
var culture = new CultureInfo("fr");
CultureInfo.CurrentUICulture = culture;
}
}NOTE
The required
resxfiles withBuild ActionasEmbeddedResource(File name should contain culture code) into theResourcesfolder.
Add Localization resx file in sample level
Follow these steps to localize the DataForm based on the CurrentUICulture using the resource files.
-
Right-click on the
Resourcesfolder in the application.
- Click
Addand thenNewItem. -
In the
Add New Itemwizard, select theResource Fileoption and name the filename asDataFormLocalization.<culture name>.resx.For example, give the nameDataFormLocalization.fr.resxfor French culture.
-
The culture name indicates the name of the language and country.
-
Now, click
Addto add the resource file in the Resources folder.
-
Add the Name or Value pair in the Resource Designer of the
DataFormLocalization.fr.resxfile and change its corresponding value to the corresponding culture.
Localizing data form display values
Here, the display attributes or data form item display values to get localized based on culture from the Localization Resource File (.Resx).
Using attribute
The ResourceType property of the Display attribute specifies the Resources File (.Resx), which is used to localize the values of the Name, ShortName, GroupName, Prompt and ItemsSource.
[Display(Name = "FirstName", GroupName = "Name", Prompt = "PromptText", ResourceType = typeof(DataFormLocalization))]
public string FirstName { get; set; }Using event
Also, localize the DataFormItem’s LabelText, PlaceholderText, and GroupName in the GenerateDataFormItem event of the SfDataForm by using the Resources (.Resx) file.
Here, the string member of the .resx file will be accessed through the class (in resxFilename.Designer.cs), which will be auto-generated when the .resx file is created, and static string members get localized using the ResourceManager based on culture.
[Display(Name = "First Name", GroupName = "Details", Prompt = "Type first name")]
public String FirstName { get; set; }
this.dataForm.GenerateDataFormItem += OnGenerateDataFormItem;
private void OnGenerateDataFormItem(object sender, GenerateDataFormItemEventArgs e)
{
if (e.DataFormItem?.GroupName == "Details")
{
e.DataFormItem.GroupName = DataFormLocalization.GroupName;
}
if (e.DataFormItem.LabelText == "FirstName")
{
e.DataFormItem.LabelText = DataFormLocalization.FirstName;
e.DataFormItem.PlaceholderText = DataFormLocalization.PromptText;
}
}Localizing validation error messages
Here, the ValidationAttribute values or data form error messages get localized based on culture from Localization Resource File (.Resx).
Using attribute
The ValidationAttribute error message can be localized using the ErrorMessageResourceType and ErrorMessageResourceName properties, which are used to get localized error messages from the Localization Resource File (.Resx) based on culture.
[Display(Name = "FirstName", GroupName = "Name", Prompt = "PromptText", ResourceType = typeof(DataFormLocalization))]
[DataFormDisplayOptions(ValidMessage = "ValidMessage")]
[Required(ErrorMessage = "Value should not be empty", ErrorMessageResourceName = "ErrorMessage", ErrorMessageResourceType = typeof(DataFormLocalization))]
public string FirstName { get; set; }Using event
Also, localize the data form error message and valid message in the ValidateProperty event of the SfDataForm by using the Resources (.Resx) file.
[DataFormDisplayOptions(ValidMessage = "Text length is enough")]
[Required(ErrorMessage = "Enter proper name")]
[StringLength(15, ErrorMessage = "Enter proper name")]
public string LastName { get; set; }
this.dataForm.ValidateProperty += this.OnDataFormValidateProperty;
private void OnDataFormValidateProperty(object sender, DataFormValidatePropertyEventArgs e)
{
if (e.PropertyName == nameof(LocalizationModel.LastName))
{
if (e.IsValid)
{
e.ValidMessage = DataFormLocalization.ValidMessage;
}
else
{
e.ErrorMessage = DataFormLocalization.ErrorMessage;
}
}
}Localizing data form picker editors
Localize DataForm list items (Picker, AutoComplete, RadioGroup, ComboBox) ItemsSource using the ResourceType property of Display attribute.
[Display(ResourceType = typeof(DataFormLocalization))]
public Gender Gender { get; set; }
this.dataForm.RegisterEditor("Gender", DataFormEditorType.RadioGroup);
public enum Gender
{
Male,
Female,
Other
}Here, the radio group items source gets localized based on culture from Localization Resource File (.Resx).

NOTE