-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add UseScaleWord format to auto-select scale based on locale #1697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d51f389
6483a84
a503644
db38c9c
ddc0d67
1a5cdeb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -494,6 +494,16 @@ | |
| { | ||
| return UnitPrefixes[symbol].LongScaleWord; | ||
| } | ||
|
|
||
|
Check failure on line 497 in src/Humanizer/MetricNumeralExtensions.cs
|
||
| if (formatValue.HasFlag(MetricNumeralFormats.UseScaleWord)) | ||
| { | ||
| var culture = CultureInfo.CurrentUICulture; | ||
| var isLongScale = LongScaleCultures.Contains(culture.Name) | ||
| || LongScaleCultures.Contains(culture.TwoLetterISOLanguageName); | ||
| return isLongScale | ||
| ? UnitPrefixes[symbol].LongScaleWord | ||
| : UnitPrefixes[symbol].ShortScaleWord; | ||
|
Comment on lines
+498
to
+505
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle regional short-scale exceptions before the language fallback.
💡 Suggested fix+ static readonly HashSet<string> ShortScaleCultureOverrides =
+ [
+ "pt-BR"
+ ];
+
static string GetUnitText(char symbol, MetricNumeralFormats? formats)
{
if (formats.HasValue)
{
var formatValue = formats.Value;
@@
if (formatValue.HasFlag(MetricNumeralFormats.UseScaleWord))
{
var culture = CultureInfo.CurrentUICulture;
- var isLongScale = LongScaleCultures.Contains(culture.Name)
- || LongScaleCultures.Contains(culture.TwoLetterISOLanguageName);
+ var isLongScale = !ShortScaleCultureOverrides.Contains(culture.Name)
+ && (LongScaleCultures.Contains(culture.Name)
+ || LongScaleCultures.Contains(culture.TwoLetterISOLanguageName));
return isLongScale
? UnitPrefixes[symbol].LongScaleWord
: UnitPrefixes[symbol].ShortScaleWord;
}
}Also applies to: 558-565 🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
|
|
||
| return symbol.ToString(); | ||
|
|
@@ -536,4 +546,38 @@ | |
| public string ShortScaleWord { get; } = shortScaleWord; | ||
| public readonly string LongScaleWord => longScaleWord ?? ShortScaleWord; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// A set of culture codes that use the <a href="https://en.wikipedia.org/wiki/Long_and_short_scales">long scale</a> system. | ||
| /// In the long scale, <c>1E9</c> is a <c>milliard</c> and <c>1E12</c> is a <c>billion</c>. | ||
| /// Cultures not in this set are assumed to use the short scale, where <c>1E9</c> is a <c>billion</c>. | ||
| /// Used by <see cref="MetricNumeralFormats.UseScaleWord"/> to automatically select the correct scale word | ||
| /// based on <see cref="System.Globalization.CultureInfo.CurrentUICulture"/>. | ||
| /// </summary> | ||
| static readonly HashSet<string> LongScaleCultures = | ||
| [ | ||
| "de", "de-DE", "de-AT", "de-CH", // German | ||
| "fr", "fr-FR", "fr-BE", "fr-CH", // French | ||
| "it", "it-IT", "it-CH", // Italian | ||
| "es", "es-ES", // Spanish | ||
| "pt", "pt-PT", // Portuguese (NOT pt-BR - Brazil uses short scale) | ||
| "nl", "nl-NL", "nl-BE", // Dutch | ||
| "ru", "ru-RU", // Russian | ||
| "pl", "pl-PL", // Polish | ||
| "tr", "tr-TR", // Turkish | ||
| "cs", "cs-CZ", // Czech (miliarda for 1E9) | ||
| "sk", "sk-SK", // Slovak | ||
| "hr", "hr-HR", // Croatian | ||
| "ro", "ro-RO", // Romanian | ||
| "sl", "sl-SI", // Slovenian | ||
| "uk", "uk-UA", // Ukrainian | ||
| "he", "he-IL", // Hebrew (מיליארד for 1E9) | ||
| "ar", "ar-SA", // Arabic | ||
| "hu", "hu-HU", // Hungarian | ||
| "fi", "fi-FI", // Finnish | ||
| "nb", "nb-NO", // Norwegian | ||
| "sv", "sv-SE", // Swedish | ||
| "da", "da-DK", // Danish | ||
| ]; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UseScaleWordtreats a culture as long-scale when eitherculture.Nameorculture.TwoLetterISOLanguageNameis inLongScaleCultures. Because the set includes"pt", this forcespt-BRinto long-scale even though Humanizer registers a separate Brazilian Portuguese converter (NumberToWordsConverterRegistry) and that converter uses short-scale billions (BrazilianPortugueseNumberToWordsConverterwithbilhãoat 10^9). In practice,1E9.ToMetric(WithSpace | UseScaleWord)will return the wrong scale word forpt-BR.Useful? React with 👍 / 👎.