We are developing several mobile apps, with Common .NET Standard library between them, which holds the common functionallity. (MVVM) The Common project has a TranslationManager Class, and a Resource file, which holds the common translations. TranslationManager uses constructor injection, to inject the app specific translation resources.
public TranslationManager(ResourceManager appSpecificLanguageResources)
{
_commonResources = CommonTranslationResources.ResourceManager;
_appSpecificLanguageResources = appSpecificLanguageResources;
}
With this code, we earn the possibilty to use common translations, and application specific translations with using only one Translation provider.
if (string.IsNullOrWhiteSpace(translationKey))
return null;
string commonTranslation = _commonResources.GetString(translationKey, new CultureInfo(_preferenceCache.CultureName));
string appSpecificTranslation = _appSpecificLanguageResources.GetString(translationKey, new CultureInfo(_preferenceCache.CultureName));
if (commonTranslation == null && appSpecificTranslation == null)
{
MobileLogger.Instance.LogWarning($"Translate could not found by translationKey: {translationKey}");
return $"TRANSLATION_{translationKey}";
}
if (!string.IsNullOrWhiteSpace(commonTranslation) && !string.IsNullOrWhiteSpace(appSpecificTranslation))
{
MobileLogger.Instance.LogDebug(TAG, $"Warning! Duplicate translate found for '{translationKey}' translationkey. Common translate is : '{commonTranslation}' , AppSpecific Translation is: {appSpecificTranslation}. Returning with appspecific translation.");
return appSpecificTranslation;
}
if (commonTranslation == null)
return appSpecificTranslation;
else
return commonTranslation;
In XAML, we have one MarkupExtension which provides the translation for the current language.
public class TranslateMarkupExtension : IMarkupExtension
{
public TranslateMarkupExtension()
{
}
public string TranslationKey { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (string.IsNullOrWhiteSpace(TranslationKey)) return "nullref";
return Resolver.Resolve<TranslationManager>().GetTranslationByKeyForCurrentCulture(TranslationKey);
}
}
XAML Usage seem to be like:
Entry Placeholder="{extensions:TranslateMarkup TranslationKey=PlaceholderPhoneNumber}"
The problem is, when i set the language at runtime, the translation extension markup does not evaluate the new translation.
Raising propertychanged with null parameter refreshes the bindings on the view, but does not affect MarkupExtensions.
I do not want to push the same page to the navigation stack, it seems patchwork for me.