2
votes

I'm working on a Xamarin Forms app, using a .NET Standard library. I've used the TranslateExtension as mentioned in the docs. I've added three resource files:

  • AppResources.resx (with matching code-behind file, auto-generated)
  • AppResources.nl.resx (Dutch translations)
  • AppResources.fr.resx (English tranlations)

When debugging the (UWP) app, I can't get the default culture (English) to be displayed. The following line returns null:

ResMgr.Value.GetString(Text, ci);

I've add some lines for debugging, and the other two languages do return the translated value:

ResMgr.Value.GetString(Text, new CultureInfo("nl")); // OK
ResMgr.Value.GetString(Text, new CultureInfo("fr")); // OK
ResMgr.Value.GetString(Text, new CultureInfo("en")); // returns null

What could possibly be the cause of this?

Things I've tried:

  • I've copy-pasted the key across all resource files, so I've ruled out misspelling the resource key.
  • I've tested "en-GB", "en-US", CultureInfo.InvariantCulture.
  • I've changed the default lanuage in the appxmanifest from en-US to en.
3

3 Answers

4
votes

On UWP, watch that your resource name does not have a period in it..

You also likely need to tell the UWP project what its default language is. This can be done in the .csproj in the top <PropertyGroup> element (the one with no conditions) by adding <DefaultLanguage>en-US</DefaultLanguage> (using the proper culture for your scenario).

Don't forget to set the [assembly: NeutralResourcesLanguage("en-US")] as well for the assembly containing the resources - this is required for .NET Standard 2.0 and likely PCL.

0
votes

Well it seems like you've got resx files for "nl" and "fr" (which you've said works fine) but you don't have one for "en". If you're trying to get the English value like that, I would think you need an "en" file the same way you do for "nl" and "fr"

0
votes

I noticed that you told about NET Standard library

ResourceManager.GetString started giving null for new entries after moving from PCL to .NET Standard

I also observed that the file AppResources.Designer.cs disapeared from the Solution Explorer (but I don't know how, was still considered) and, in the VS res editon of AppResources.resx, Access Modifier was No Code Generation.

Here is what I did to fix that:

  • I double-clicked on AppResources.resx and set Access Modifier to Internal or Public
  • In .NET Standard you may no longer be able to see "View Code" from resx files; so, I righ-clicked on AppResources.resx-> Open With -> Source Code Editor and copy the new created entries to all the others *.fr (also *.nl in your case) ...

NOTE

  1. In the context menu you should see Run Custom Tool. You may need to delete the old AppResources.Designer.cs and do "Run Custom Tool"
  2. Don't forget to clear the solution (or olny UWP and the standard library), close VS, delete all .\bin and .\obj and restart VS. This to ensure your issue is fixed or not using the very very new generated true code :)