- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello community,
I am struggling to get my C# code correct to convert my user forms into each culture. I have tried for days, but I cannot get my labels to change languages when opening Revit in different language versions. I can get my Ribbon Panel button to change languages using a RibbonResources.resx file in my App.cs, but my FormExport.resx files are not providing language translation values.
public FormExport(Autodesk.Revit.DB.Document doc)
{
CultureInfo cultureName = new CultureInfo(Thread.CurrentThread.CurrentUICulture.Name);
string cultureRef = cultureName.Name;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureRef);
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureRef);
InitializeComponent();
familyDocument = doc;
// Get the localized label text from the resource .resx file
string localizedLabelText = GetLocalizedTextFromResource("lblFamilyName.Text");
string localizedHeaderText = GetLocalizedTextFromResource("lblHeader.Text");
// Set the label text with the localized value
lblFamilyName.Text = localizedLabelText;
lblHeader.Text = localizedHeaderText;
}
I have this function set below my form code :
private string GetLocalizedTextFromResource(string key)
{
try
{
// Load the appropriate resource file based on the user's selected language
ResourceManager resourceManager = new ResourceManager("MyApp.FormExport", typeof(FormExport).Assembly);
CultureInfo currentCulture = Thread.CurrentThread.CurrentUICulture;
// Fetch the localized text for the given key from the resource
string localizedText = resourceManager.GetString(key, currentCulture);
// If the resource for the given key is not found in the selected culture,
// explicitly load the default resource (English) using CultureInfo.InvariantCulture
if (localizedText == null)
{
localizedText = resourceManager.GetString(key, CultureInfo.InvariantCulture);
}
// If the resource is still not found, return the key itself as a fallback
return localizedText ?? key;
}
catch (MissingManifestResourceException ex)
{
// Log the exception
Console.WriteLine($"Resource file not found. Exception: {ex.Message}");
return key; // Return the key itself as a fallback
}
}
My form .resx files are:
FormExport.resx
FormExport.en.resx
FormExport.fr.resx
.....(continued)
Thank you for any assistance you can provide.
Geoff
Solved! Go to Solution.