Ubuntu 上部署的 .NET Core 应用程序的区域设置问题
Locale issue with .NET Core application deployed on Ubuntu
我已经将我的 .NET Core (v2.1) 应用程序部署到我的 Ubuntu 服务器 (Ubuntu 18.04 LTS) 上。应用程序目标受众是英国人。
在 C# 中我正在做:
@invoice.Amount.ToString("C")
根据系统文化将值格式化为货币,应该显示类似 £107.50
的内容,但我得到 7.50
.
我检查了语言环境,我有 en_US
所以我 运行 update-locale LANG=en_GB.utf8
然后我重新启动了一切(kestrel、nginx 和会话)。现在当我 运行 locale
命令时,我得到:
LANG=en_GB.utf8
LANGUAGE=
LC_CTYPE="en_GB.utf8"
LC_NUMERIC="en_GB.utf8"
LC_TIME="en_GB.utf8"
LC_COLLATE="en_GB.utf8"
LC_MONETARY="en_GB.utf8"
LC_MESSAGES="en_GB.utf8"
LC_PAPER="en_GB.utf8"
LC_NAME="en_GB.utf8"
LC_ADDRESS="en_GB.utf8"
LC_TELEPHONE="en_GB.utf8"
LC_MEASUREMENT="en_GB.utf8"
LC_IDENTIFICATION="en_GB.utf8"
LC_ALL=
但是,我仍然得到 7.50
而不是 £107.50
。我错过了什么?
阅读Standard Numeric Format Strings(例子在C#
):
Standard numeric format strings are supported by some overloads of the
ToString
method of all numeric types. For example, you can supply a
numeric format string to the Int32.ToString(FormatSpecifier)
and
Int32.ToString(FormatSpecifier, IFormatProvider)
methods…
所以.ToString()
方法应该接受第二个参数IFormatProvider
(i.e. an object that supplies culture-specific formatting information) in addition to the Currency ("C"
) Format Specifier,试试
@invoice.Amount.ToString("C",CultureInfo.CurrentCulture)
或
@invoice.Amount.ToString("C",CultureInfo.CurrentUICulture)
或
@invoice.Amount.ToString("C",CultureInfo.GetCultureInfo('en-GB'))
或
@invoice.Amount.ToString("C",CultureInfo.CreateSpecificCulture('en-GB'))
(可能需要 using System.Globalization
)。
我目前无法提供 运行 C#
示例。但是,我可以举例说明在 PowerShell 中 System.Globalization.CultureInfo
class 的(一些)static 属性和方法的使用:
PS D:\PShell> 1234.578.ToString('C',[cultureinfo]::CurrentCulture)
1 234,58 Kč
PS D:\PShell> 1234.578.ToString('C',[cultureinfo]::CurrentUICulture)
£1,234.58
PS D:\PShell> 1234.578.ToString('C',[cultureinfo]::GetCultureInfo('en-US'))
,234.58
PS D:\PShell> 1234.578.ToString('C',[cultureinfo]::CreateSpecificCulture('de-DE'))
1.234,58 €
我已经将我的 .NET Core (v2.1) 应用程序部署到我的 Ubuntu 服务器 (Ubuntu 18.04 LTS) 上。应用程序目标受众是英国人。
在 C# 中我正在做:
@invoice.Amount.ToString("C")
根据系统文化将值格式化为货币,应该显示类似 £107.50
的内容,但我得到 7.50
.
我检查了语言环境,我有 en_US
所以我 运行 update-locale LANG=en_GB.utf8
然后我重新启动了一切(kestrel、nginx 和会话)。现在当我 运行 locale
命令时,我得到:
LANG=en_GB.utf8
LANGUAGE=
LC_CTYPE="en_GB.utf8"
LC_NUMERIC="en_GB.utf8"
LC_TIME="en_GB.utf8"
LC_COLLATE="en_GB.utf8"
LC_MONETARY="en_GB.utf8"
LC_MESSAGES="en_GB.utf8"
LC_PAPER="en_GB.utf8"
LC_NAME="en_GB.utf8"
LC_ADDRESS="en_GB.utf8"
LC_TELEPHONE="en_GB.utf8"
LC_MEASUREMENT="en_GB.utf8"
LC_IDENTIFICATION="en_GB.utf8"
LC_ALL=
但是,我仍然得到 7.50
而不是 £107.50
。我错过了什么?
阅读Standard Numeric Format Strings(例子在C#
):
Standard numeric format strings are supported by some overloads of the
ToString
method of all numeric types. For example, you can supply a numeric format string to theInt32.ToString(FormatSpecifier)
andInt32.ToString(FormatSpecifier, IFormatProvider)
methods…
所以.ToString()
方法应该接受第二个参数IFormatProvider
(i.e. an object that supplies culture-specific formatting information) in addition to the Currency ("C"
) Format Specifier,试试
@invoice.Amount.ToString("C",CultureInfo.CurrentCulture)
或@invoice.Amount.ToString("C",CultureInfo.CurrentUICulture)
或@invoice.Amount.ToString("C",CultureInfo.GetCultureInfo('en-GB'))
或@invoice.Amount.ToString("C",CultureInfo.CreateSpecificCulture('en-GB'))
(可能需要 using System.Globalization
)。
我目前无法提供 运行 C#
示例。但是,我可以举例说明在 PowerShell 中 System.Globalization.CultureInfo
class 的(一些)static 属性和方法的使用:
PS D:\PShell> 1234.578.ToString('C',[cultureinfo]::CurrentCulture)
1 234,58 Kč
PS D:\PShell> 1234.578.ToString('C',[cultureinfo]::CurrentUICulture)
£1,234.58
PS D:\PShell> 1234.578.ToString('C',[cultureinfo]::GetCultureInfo('en-US'))
,234.58
PS D:\PShell> 1234.578.ToString('C',[cultureinfo]::CreateSpecificCulture('de-DE'))
1.234,58 €