Reputation: 183
I've this application structure:
AppName
Resources
Languages
en.xaml
it.xaml
I'm trying to load a specific ResourceDictionary
based on the CurrentCulture
of the software that I set when the application start, a little example:
var dict = new ResourceDictionary();
string currentLang = "it-IT";
if (currentLang == "it-IT")
{
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("it");
Application.LoadComponent(dict, new Uri("..\\Resources\\Languages\\it.xaml", UriKind.Relative));
}
else
{
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en");
Application.LoadComponent(dict, new Uri("..\\Resources\\Languages\\en.xaml", UriKind.Relative));
}
when the code reach this line: Application.LoadComponent(dict, new Uri("..\\Resources\\Languages\\it.xaml", UriKind.Relative));
I get this error:
System.Exception: 'A resource identified by the URI' ..\Resources\Languages\it.xaml 'is not available for the' System.Windows.ResourceDictionary 'component.'
why happen this?
Thanks for any explaination.
Upvotes: 2
Views: 92
Reputation: 7325
This should work:
string currentLang = "it-IT";
if (currentLang == "it-IT")
{
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("it");
MainWindow.AppWindow.lsVm.GetCurrentLangDict=(ResourceDictionary)Application.LoadComponent(new Uri("..\\Resources\\Languages\\it.xaml", UriKind.Relative));
}
else
{
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en");
MainWindow.AppWindow.lsVm.GetCurrentLangDict=(ResourceDictionary)Application.LoadComponent(new Uri("..\\Resources\\Languages\\en.xaml", UriKind.Relative));
}
Upvotes: 0
Reputation: 169370
This should load the resource dictionary and merge into your App.xaml
:
ResourceDictionary dict = new ResourceDictionary() { Source = new Uri("/Resources/Languages/it.xaml", UriKind.Relative) };
Application.Current.Resources.MergedDictionaries.Add(dict);
Upvotes: 0
Reputation: 495
Try it like this. I am doing like this and it finds them.
Application.LoadComponent(dict, new Uri("pack:\\application:,,,\YourDLL;\\component\\Resources\\Languages\\en.xaml", UriKind.Relative));
Upvotes: 1