Skip to content

Commit

Permalink
Tweak a localization to remove any assumption on "+" localization
Browse files Browse the repository at this point in the history
See discussion at
#19472 (comment)
  • Loading branch information
mokagio committed Oct 17, 2022
1 parent 47c1500 commit 7e7210d
Showing 1 changed file with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,45 @@ struct StatsSegmentedControlData {
// We want to show something like "+1.2K (5%)" if we have a percentage difference and "1.2K" if we don't.
// Because localized strings need to be strings literal, we cannot embed any conditional logic in the `localizedString...` call.
// We therefore need to generate different string literals based on the state.
let differenceSign = difference < 0 ? "" : "+"
if differencePercent != 0 {
let stringFormat = NSLocalizedString(
"insights.visitorsLineChartCell.differenceLabelWithPercentage",
value: "%@%@ (%@%%)",
switch (difference > 0, differencePercent != 0) {
case (true, true): // E.g.: +1.2k (5%)
stringFormat = NSLocalizedString(
"insights.visitorsLineChartCell.differenceLabelPosiviteWithPercentage",
value: "+%@ (%@%%)",
comment: "Difference label for Insights Overview stat, indicating change from previous period, including percentage value. Example: +99.9K (5%)"
)
return String.localizedStringWithFormat(
stringFormat,
differenceSign,
difference.abbreviatedString(),
differencePercent.abbreviatedString()
)
} else {
let stringFormat = NSLocalizedString(
"insights.visitorsLineChartCell.differenceLabelWithoutPercentage",
value: "%@%@",
case (true, false): // E.g.: +1.2k
// We cannot assume every locale would translate an English string like "+1.2k" in the same way.
// So, even though we have only a "+" prefix, we ought to make this string localized.
stringFormat = NSLocalizedString(
"insights.visitorsLineChartCell.differenceLabelPosiviteWithoutPercentage",
value: "+%@",
comment: "Difference label for Insights Overview stat, indicating change from previous period. Example: +99.9K"
)
return String.localizedStringWithFormat(
stringFormat,
differenceSign,
difference.abbreviatedString()
)
case (false, true): // E.g. 1.2k (5%)
stringFormat = NSLocalizedString(
"insights.visitorsLineChartCell.differenceLabelNotPosiviteWithPercentage",
value: "%@ (%@%%)",
comment: "Difference label for Insights Overview stat, indicating change from previous period, including percentage value, when the change is 0 or less. Example: 99.9K (5%)"
)
return String.localizedStringWithFormat(
stringFormat,
difference.abbreviatedString(),
differencePercent.abbreviatedString()
)
break
case (false, false): // E.g.: 1.2k
// There's no + sign nor percentage value here, we don't need to add any localization treatment.
return difference.abbreviatedString()
}
}

Expand Down

0 comments on commit 7e7210d

Please sign in to comment.