I have applied a fairly simple workaround in my app. The silverlight app is bilingual: the resx strings are en-US with a satellite assembly Dutch nl-NL. However, as the host application supports 5 languages, the workaround should be suitable for more than two languages and handle the fallback to English.
1. Open the nl-NL satellite resx file in notepad++ or similar
2. Give all string ID's a postfix _nl, where nl is the 2-letter language code. If your app supports regional languages, use _nl_NL or language ID like _0413 instead.
For me it was replace [" xml:space="preserve">] by [_nl" xml:space="preserve">]
3. Copy the data part of the satellite strings table to the end of the main resx strings table.
Because I used a postfix, the strings will be sorted by ID, then language in the VS resource editor. This is more convenient than prefix sort if you need to correct translations or add new strings:
The system works if you read strings in code-behind. My app uses a common function, that I changed to handle the new language postfix. In the CSHTML5-version, I pass a 2-letter language ID in a query parameter and store it in the new Language field, the Silverlight version ignores the new field and leaves it as "EN":
Code: Select all
public static string Language = "EN";
...
public static string GetResource(string sResourceKey)
{
string sResource = string.Empty;
try
{
if (Language != "EN")
sResource = Resources.Strings.ResourceManager.GetString(sResourceKey + "_" + Language.ToLower());
}
catch { }
try
{
if (string.IsNullOrEmpty(sResource))
sResource = Resources.Strings.ResourceManager.GetString(sResourceKey);
}
catch (Exception ex) { Common.SendLogToServer(ex); }
return sResource;
}
The first try/catch or the check for empty string below cause a fall-back to English if the localized string does not exist, so that the behavior for missing strings is the same as in Silverlight or WPF. As the Silverlight version never sets the Language field, the same code can still exist in both versions of the app (I expect to have to maintain the Silverlight version for at least a year after release of the CSHTML5 equivalent).
To see it in action: