将日期格式更改为 en-us,而文化为 fr-ca

发布于 08-26 15:58 字数 99 浏览 4 评论 0原文

我正在致力于法语网站的本地化。但是我不应该将日期格式更改为法语。即使区域性设置为 fr-ca,它也必须保持 en-us 格式,即,当其余内容为法语时,日期格式仍应为英语(en-us)。

I'm working on localizing a website in French. However I am not supposed to change the date format to French. It must remain as per en-us format even if the culture is set to fr-ca i.e, when rest of the contents are in French, the date format should still be in English(en-us).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

Oo萌小芽oO2024-09-02 15:58:45

要更改日期的格式,您可以创建自定义 CultureInfo,基于现有的 CultureInfo(在您的情况下为“fr-CA”),仅修改日期格式。我在这方面没有任何经验,但链接的文章和这篇文章 解释了它是如何完成的。据说,也不是太难。

我想将 System.Threading.Thread.CurrentThread.CurrentCulture 设置为自定义 CultureInfo 的实例(例如在 Page.Load 事件中)应该可以完成这项工作。


或者,使用 CultureInfo 类基于每个字符串指定区域性:

CultureInfo culture = new CultureInfo("en-US");

每当向页面写入日期时,请使用以下语法:

myDate.ToString("d", culture);

string.Format(
  culture,
  "This is a string containing a date: {0:d}",
  myDate);

CultureInfo 类驻留在 System.Globalization 命名空间中,并且 d<上面的/code>是输出日期的格式。 请参阅 John Sheehan 的“.NET 格式字符串快速参考”备忘单,了解有关格式字符串的更多信息。

To change how dates are formatted you could create a custom CultureInfo, based on an existing CultureInfo (in your case "fr-CA"), modifying only the date formats. I don't have any experience in this, but the linked aricle and this article explains how it's done. Supposedly, it's not too difficult.

I imagine that setting System.Threading.Thread.CurrentThread.CurrentCulture to an instance of your custom CultureInfo (e.g. in the Page.Load event) should do the job.


Or, use the CultureInfo class to specify culture on a per-string basis:

CultureInfo culture = new CultureInfo("en-US");

Whenever you write a date to the page, use the following syntax:

myDate.ToString("d", culture);

or

string.Format(
  culture,
  "This is a string containing a date: {0:d}",
  myDate);

The CultureInfo class resides in the System.Globalization namespace and d in the above is the format in which to output the date. See John Sheehan's ".NET Format String Quick Reference" cheat sheet for more on format strings.

我做我的改变2024-09-02 15:58:45

就我而言,我必须设置应用程序的语言,还确定该语言是否是从右到左的语言,但还需要保留标准日期时间格式。这就是我所做的:

string culture = "ar-SA";  
//Set language and culture to Arabic  
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(culture);

//But independent of language, keep datetime format same
DateTimeFormatInfo englishDateTimeFormat = new CultureInfo("en-CA").DateTimeFormat;
Thread.CurrentThread.CurrentCulture.DateTimeFormat = englishDateTimeFormat;

In my case I had to set the language of the app, also determine if the language is right-to-left language, but also needed to keep the standard datetime format. So that was what I did:

string culture = "ar-SA";  
//Set language and culture to Arabic  
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(culture);

//But independent of language, keep datetime format same
DateTimeFormatInfo englishDateTimeFormat = new CultureInfo("en-CA").DateTimeFormat;
Thread.CurrentThread.CurrentCulture.DateTimeFormat = englishDateTimeFormat;
深居我梦2024-09-02 15:58:45

在字符串上,当您显示日期时,请执行以下操作:

CultureInfo ci = new CultureInfo("en-US");
string s = dateTimeObject.ToString(ci);

这是一个简化的示例,但是您只需对 DateTime 对象执行所需的操作即可。

On the string, when you display the date, do the following:

CultureInfo ci = new CultureInfo("en-US");
string s = dateTimeObject.ToString(ci);

This is a simplified example however, you just need to do the necessary work you want to do on the DateTime object.

随遇而安2024-09-02 15:58:45

谢谢各位!!!!
看来你的建议对我有用。
我尝试创建一种自定义文化,扩展 fr-ca 的功能并将其日期格式更改为 en-us。
这是代码

CultureInfo ci = new CultureInfo("fr-ca");
DateTimeFormatInfo dateformat = new DateTimeFormatInfo();
dateformat.FullDateTimePattern = "dddd, mmmm dd, yyyy h:mm:ss tt";// Date format of en-us
ci.DateTimeFormat = dateformat;
CultureAndRegionInfoBuilder obj = new CultureAndRegionInfoBuilder("fr-ca", CultureAndRegionModifiers.Replacement);
obj.LoadDataFromCultureInfo(ci);
obj.Register();

一旦代码注册了新的 fr-ca,fr-ca 的日期格式将与 en-us 的日期格式相同。
该代码可以在Page_Load中使用。

Thanks Guys !!!!
Seems like your sugessions are working for me.
I tried creating a custom culture which extends the features of fr-ca and changes its date format to en-us.
Here is the code

CultureInfo ci = new CultureInfo("fr-ca");
DateTimeFormatInfo dateformat = new DateTimeFormatInfo();
dateformat.FullDateTimePattern = "dddd, mmmm dd, yyyy h:mm:ss tt";// Date format of en-us
ci.DateTimeFormat = dateformat;
CultureAndRegionInfoBuilder obj = new CultureAndRegionInfoBuilder("fr-ca", CultureAndRegionModifiers.Replacement);
obj.LoadDataFromCultureInfo(ci);
obj.Register();

Once the code registers new fr-ca, the date format of the fr-ca will be same as that of en-us.
The code can be used in Page_Load.

灰色世界里的红玫瑰2024-09-02 15:58:45

这是代码片段中的上述代码:

CultureInfo ci = new CultureInfo("fr-ca"); 

DateTimeFormatInfo dateformat = new DateTimeFormatInfo();



dateformat.FullDateTimePattern = "dddd, mmmm dd, yyyy h:mm:ss tt";// Date format of en-us



ci.DateTimeFormat = dateformat; 

CultureAndRegionInfoBuilder obj = new CultureAndRegionInfoBuilder("fr-ca", CultureAndRegionModifiers.Replacement);


 obj.LoadDataFromCultureInfo(ci); 

obj.Register();

Here is the above code in code snipet :

CultureInfo ci = new CultureInfo("fr-ca"); 

DateTimeFormatInfo dateformat = new DateTimeFormatInfo();



dateformat.FullDateTimePattern = "dddd, mmmm dd, yyyy h:mm:ss tt";// Date format of en-us



ci.DateTimeFormat = dateformat; 

CultureAndRegionInfoBuilder obj = new CultureAndRegionInfoBuilder("fr-ca", CultureAndRegionModifiers.Replacement);


 obj.LoadDataFromCultureInfo(ci); 

obj.Register();
迟到的我2024-09-02 15:58:45

令人惊讶的是我得到了一个非常简单的答案。在设置 Culture 和 UICulture 时,我所需要做的就是始终将 Culture 属性设置为 en-us 。这将导致始终以英文显示日期格式。

Surprisingly I got a very simple answer. While setting Culture and UICulture, all I need to do is to set the Culture Property to en-us always. This will result in showing the date format in English always.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文