gpt4 book ai didi

c# - 使用包装的语言资源 Xamarin Forms 更改语言运行时

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:52 24 4
gpt4 key购买 nike

我们正在开发几个移动应用程序,它们之间有通用的 .NET 标准库,具有通用的功能。 (MVVM)Common 项目有一个 TranslationManager 类和一个包含通用翻译的资源文件。TranslationManager 使用构造函数注入(inject)来注入(inject)应用程序特定的翻译资源。

    public TranslationManager(ResourceManager appSpecificLanguageResources)
{
_commonResources = CommonTranslationResources.ResourceManager;
_appSpecificLanguageResources = appSpecificLanguageResources;
}

通过此代码,我们获得了使用通用翻译和仅使用一个翻译提供商的应用程序特定翻译的可能性。

            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;

在 XAML 中,我们有一个 MarkupExtension,它提供当前语言的翻译。

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 用法似乎是这样的:

  Entry Placeholder="{extensions:TranslateMarkup TranslationKey=PlaceholderPhoneNumber}"

问题是,当我在运行时设置语言时,翻译扩展标记不会评估新翻译。

使用 null 参数提升 propertychanged 会刷新 View 上的绑定(bind),但不会影响 MarkupExtensions。

我不想将同一个页面推送到导航堆栈,这对我来说似乎是拼凑而成。

最佳答案

The problem is, when i set the language at runtime, the translation extension markup does not evaluate the new translation.

您可能需要为 TranslationManager 使用 INotifyPropertychanged 接口(interface),当您更改 UI 文化时,所有绑定(bind)到索引的字符串都会更新。

更详细的信息,请引用:

Xamarin.Forms change UI language at runtime (XAML)

关于c# - 使用包装的语言资源 Xamarin Forms 更改语言运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57636734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com