C# 如何以xamarin形式动态更改CultureInfo
我需要创建一个支持多语言的应用程序 所以我做了一个如下的示例应用程序 Page.xamalC# 如何以xamarin形式动态更改CultureInfo,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我需要创建一个支持多语言的应用程序 所以我做了一个如下的示例应用程序 Page.xamal <StackLayout VerticalOptions="CenterAndExpand"> <Label Text="{x:Static local:AppResources.Title}" TextColor="Black" Horizonta
<StackLayout
VerticalOptions="CenterAndExpand">
<Label Text="{x:Static local:AppResources.Title}" TextColor="Black"
HorizontalOptions="CenterAndExpand" />
<Button Text="{x:Static local:AppResources.ClickMe}" Clicked="Button1_Clicked"/>
<Label Text="{x:Static local:AppResources.Title}" TextColor="Black"
HorizontalOptions="CenterAndExpand" />
<Button Text="{x:Static local:AppResources.ClickMe}" Clicked="Button2_Clicked"/>
</StackLayout>
正如xmarin表单文档提供的那样,我设置了AssemblyInfo.cs(公共文件夹)
所以我的默认语言是“en-GB”
我有3个附件
我在这里遗漏了什么?在CultureInfo中传递参数以获取您要设置的区域性信息,例如,在我的示例中,我将其设置为德语
CultureInfo ci = new CultureInfo("de-DE");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
关于更改当前的cultureinfo,我建议您可以尝试使用Plugin.multi-language来获取它 首先,通过nuget软件包安装Plugin.Multilingual,定义.resx文件如下: 默认情况下,在常量ResourceId中的TranslateExtension.cs文件中,将假定您的资源文件添加到项目的根目录中,并且resx文件命名为AppResources。如果您将其添加到文件夹或以不同的方式命名resx文件,则可以在那里对其进行更改
public class TranslateExtension : IMarkupExtension
{
const string ResourceId = "MultilingualSample.AppResources";
static readonly Lazy<ResourceManager> resmgr = new Lazy<ResourceManager>(() => new ResourceManager(ResourceId, typeof(TranslateExtension).GetTypeInfo().Assembly));
public string Text { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Text == null)
return "";
var ci = CrossMultilingual.Current.CurrentCultureInfo;
var translation = resmgr.Value.GetString(Text, ci);
if (translation == null)
{
#if DEBUG
throw new ArgumentException(
String.Format("Key '{0}' was not found in resources '{1}' for culture '{2}'.", Text, ResourceId, ci.Name),
"Text");
#else
translation = Text; // returns the key, which GETS DISPLAYED TO THE USER
#endif
}
return translation;
}
}
公共类TranslateExtension:IMarkupExtension
{
const string ResourceId=“multilingalsample.AppResources”;
static readonly Lazy resmgr=new Lazy(()=>new ResourceManager(ResourceId,typeof(TranslateExtension.GetTypeInfo().Assembly));
公共字符串文本{get;set;}
公共对象ProviderValue(IServiceProvider服务提供者)
{
if(Text==null)
返回“”;
var ci=CrossMultilanguage.Current.CurrentCultureInfo;
var translation=resmgr.Value.GetString(文本,ci);
if(translation==null)
{
#如果调试
抛出新的ArgumentException(
String.Format(“在区域性“{2}”的资源“{1}”中找不到键“{0}”)、Text、ResourceId、ci.Name),
“文本”);
#否则
translation=Text;//返回向用户显示的键
#恩迪夫
}
返回翻译;
}
}
更多详细信息,请查看:
这是否回答了您的问题?我目前正在使用它,它在调试模式下工作,但不是在发布模式下。你能给我你的样品吗
CultureInfo ci = new CultureInfo("de-DE");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
public class TranslateExtension : IMarkupExtension
{
const string ResourceId = "MultilingualSample.AppResources";
static readonly Lazy<ResourceManager> resmgr = new Lazy<ResourceManager>(() => new ResourceManager(ResourceId, typeof(TranslateExtension).GetTypeInfo().Assembly));
public string Text { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Text == null)
return "";
var ci = CrossMultilingual.Current.CurrentCultureInfo;
var translation = resmgr.Value.GetString(Text, ci);
if (translation == null)
{
#if DEBUG
throw new ArgumentException(
String.Format("Key '{0}' was not found in resources '{1}' for culture '{2}'.", Text, ResourceId, ci.Name),
"Text");
#else
translation = Text; // returns the key, which GETS DISPLAYED TO THE USER
#endif
}
return translation;
}
}