Reputation: 2824
I am localising a Xamarin Forms project.
I am using C#'s Resx
file to achieve it.
Everything is working fine in the Sample project which I downloaded as well as the Demo project which I have created. But in the Actual project of mine, both AppResources.xyzKey
and resourceManager.GetString(xyzKey, anyCulturePassed)
returns a value from the English Resx
file.
I have even tried sending hardcoded Spanish culture to the resourceManager.GetString()
function.
const string ResourceId = "projectName.Resources.Resx.AppResources";
public static string Localize(string key, string comment)
{
//var netLanguage = Locale ();
// Platform-specific
ResourceManager temp = new ResourceManager(ResourceId, typeof(L10n).GetTypeInfo().Assembly);
Debug.WriteLine("Localize " + key);
string result = "";
try
{
//Dynamically detecting culture is commented for testing.
//CultureInfo ci = DependencyService.Get<ILocale>().GetCurrentCultureInfo();
CultureInfo ci = new CultureInfo("es");
result = temp.GetString(key, ci);
if (result == null)
{
result = key; // HACK: return the key, which GETS displayed to the user
}
return result;
}
catch (Exception e)
{
if (e != null)
{
}
}
return result;
}
In spite of this is always picks the Value from the English Resx
file.
These are my resource files:
I have ensured that the respective resource files are loaded by printing them;
var assembly = typeof(AppResources).GetTypeInfo().Assembly;
foreach (var res in assembly.GetManifestResourceNames())
{
System.Diagnostics.Debug.WriteLine("found resource: " + res);
}
This is the output:
Surprisingly, on the same platform it works just fine in the Sample as well as the Demo project.
Upvotes: 2
Views: 620
Reputation: 2824
Turns out only the English Resx
file was set as Embedded Resource
.
Hence, even on printing the GetManifestResourceNames()
, it printed the name of the Resx
file but it was only for the English version of the file.
To fix this, when you create a new XML
file as Resx
for a new language,
Right click on the file --> Build Action --> Set as Embedded Resource.
Upvotes: 1
Reputation: 1351
Resource files are influenced by the executing thread's culture. Did you try changing the running thread such as;
newCulture = CultureInfo.CreateSpecificCulture("tr-TR");
Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = newCulture;
Upvotes: 0