Skip to content

Commit 684f127

Browse files
committed
[FIX] Fix localization in CF part 2
Task: 6089166
1 parent 7c6ed6d commit 684f127

14 files changed

Lines changed: 74 additions & 214 deletions

File tree

src/actions/insert_actions.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { functionRegistry } from "../functions";
22
import { isDefined } from "../helpers";
3-
import { localizeDataValidationRule } from "../helpers/locale";
43
import { handlePasteResult } from "../helpers/ui/paste_interactive";
54
import { _t } from "../translation";
65
import { ActionBuilder, ActionSpec } from "./action";
@@ -313,7 +312,7 @@ export const insertDropdown: ActionSpec = {
313312
return;
314313
}
315314
env.openSidePanel("DataValidationEditor", {
316-
rule: localizeDataValidationRule(rule, env.model.getters.getLocale()),
315+
rule,
317316
onExit: () => {
318317
env.openSidePanel("DataValidation");
319318
},

src/components/composer/composer/abstract_composer_store.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export abstract class AbstractComposerStore extends SpreadsheetStore {
9797
}
9898
protected abstract confirmEdition(content: string): void;
9999
protected abstract getComposerContent(position: CellPosition): string;
100+
// We need to use different canonicalize method in the standalone composer and the cell composer where dates should not be converted (they are not canonicals on UPDATE_CELL)
100101
protected abstract getCurrentCanonicalContent(): string;
101102

102103
abstract stopEdition(direction?: Direction): void;

src/components/side_panel/conditional_formatting/cf_editor/cf_editor.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
isColorValid,
1414
rangeReference,
1515
} from "../../../../helpers";
16+
import { canonicalizeContent, localizeContent } from "../../../../helpers/locale";
1617
import { cycleFixedReference } from "../../../../helpers/reference_type";
1718
import { _t } from "../../../../translation";
1819
import {
@@ -370,6 +371,11 @@ export class ConditionalFormattingEditor extends Component<Props, SpreadsheetChi
370371
this.state.openedMenu = undefined;
371372
}
372373

374+
localizeValue(value: string | undefined): string {
375+
const locale = this.env.model.getters.getLocale();
376+
return value ? localizeContent(value, locale) : "";
377+
}
378+
373379
/*****************************************************************************
374380
* Cell Is Rule
375381
****************************************************************************/
@@ -503,7 +509,9 @@ export class ConditionalFormattingEditor extends Component<Props, SpreadsheetChi
503509
}
504510

505511
updateThresholdValue(threshold: "minimum" | "midpoint" | "maximum", value: string) {
506-
this.state.rules.colorScale[threshold]!.value = value;
512+
const locale = this.env.model.getters.getLocale();
513+
const canonicalizedValue = canonicalizeContent(value, locale);
514+
this.state.rules.colorScale[threshold]!.value = canonicalizedValue;
507515
this.updateConditionalFormat({ rule: this.state.rules.colorScale });
508516
}
509517

@@ -565,7 +573,9 @@ export class ConditionalFormattingEditor extends Component<Props, SpreadsheetChi
565573
inflectionPoint: "lowerInflectionPoint" | "upperInflectionPoint",
566574
value: string
567575
) {
568-
this.state.rules.iconSet[inflectionPoint].value = value;
576+
const locale = this.env.model.getters.getLocale();
577+
const canonicalizedValue = canonicalizeContent(value, locale);
578+
this.state.rules.iconSet[inflectionPoint].value = canonicalizedValue;
569579
this.updateConditionalFormat({ rule: this.state.rules.iconSet });
570580
}
571581

src/components/side_panel/conditional_formatting/cf_editor/color_scale_rule_editor.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
t-if="threshold.type !== 'formula'"
4242
type="text"
4343
class="o-input"
44-
t-att-value="threshold.value"
44+
t-att-value="this.localizeValue(threshold.value)"
4545
t-on-change="(ev) => this.updateThresholdValue(thresholdType, ev.target.value)"
4646
t-att-class="{ 'o-invalid': isValueInvalid(thresholdType), 'invisible': threshold === undefined }"
4747
/>

src/components/side_panel/conditional_formatting/cf_editor/icon_set_rule_editor.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
t-if="inflectionPointValue.type !== 'formula'"
5858
class="o-input"
5959
t-att-class="{ 'o-invalid': isInflectionPointInvalid(inflectionPoint) }"
60-
t-att-value="rule[inflectionPoint].value"
60+
t-att-value="this.localizeValue(rule[inflectionPoint].value)"
6161
t-on-change="(ev) => this.setInflectionValue(inflectionPoint, ev.target.value)"
6262
/>
6363
<StandaloneComposer t-else="" t-props="getColorIconSetComposerProps(inflectionPoint)"/>

src/components/side_panel/conditional_formatting/cf_preview/cf_preview.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, useRef } from "@odoo/owl";
22
import { CF_ICON_EDGE_LENGTH, GRAY_200, GRAY_300, HIGHLIGHT_COLOR } from "../../../../constants";
33
import { colorNumberString } from "../../../../helpers";
4+
import { localizeContent } from "../../../../helpers/locale";
45
import { _t } from "../../../../translation";
56
import { ConditionalFormat, Highlight, SpreadsheetChildEnv } from "../../../../types";
67
import { cellStyleToCss, css, cssPropertiesToCss } from "../../../helpers";
@@ -111,10 +112,15 @@ export class ConditionalFormatPreview extends Component<Props, SpreadsheetChildE
111112
case "CellIsRule":
112113
const description = CellIsOperators[cf.rule.operator];
113114
if (cf.rule.values.length === 1) {
114-
return `${description} ${cf.rule.values[0]}`;
115+
const locale = this.env.model.getters.getLocale();
116+
const localizedValue = localizeContent(cf.rule.values[0], locale);
117+
return `${description} ${localizedValue}`;
115118
}
116119
if (cf.rule.values.length === 2) {
117-
return _t("%s %s and %s", description, cf.rule.values[0], cf.rule.values[1]);
120+
const locale = this.env.model.getters.getLocale();
121+
const localizedValue1 = localizeContent(cf.rule.values[0], locale);
122+
const localizedValue2 = localizeContent(cf.rule.values[1], locale);
123+
return _t("%s %s and %s", description, localizedValue1, localizedValue2);
118124
}
119125
return description;
120126
case "ColorScaleRule":

src/components/side_panel/conditional_formatting/conditional_formatting.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Component, onWillUpdateProps, useState } from "@odoo/owl";
2-
import { localizeCFRule } from "../../../helpers/locale";
32
import { ConditionalFormat, SpreadsheetChildEnv, UID, Zone } from "../../../types";
43
import { Section } from "../components/section/section";
54
import { ConditionalFormattingEditor } from "./cf_editor/cf_editor";
@@ -71,10 +70,7 @@ export class ConditionalFormattingPanel extends Component<Props, SpreadsheetChil
7170
const cfs = this.env.model.getters.getConditionalFormats(
7271
this.env.model.getters.getActiveSheetId()
7372
);
74-
return cfs.map((cf) => ({
75-
...cf,
76-
rule: localizeCFRule(cf.rule, this.env.model.getters.getLocale()),
77-
}));
73+
return cfs;
7874
}
7975

8076
private switchToList() {

src/components/side_panel/data_validation/data_validation_panel.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Component, useState } from "@odoo/owl";
2-
import { localizeDataValidationRule } from "../../../helpers/locale";
32
import { DataValidationRule, SpreadsheetChildEnv, UID } from "../../../types";
43
import { DataValidationEditor } from "./dv_editor/dv_editor";
54
import { DataValidationPreview } from "./dv_preview/dv_preview";
@@ -41,12 +40,6 @@ export class DataValidationPanel extends Component<Props, SpreadsheetChildEnv> {
4140
this.state.activeRule = undefined;
4241
}
4342

44-
localizeDVRule(rule?: DataValidationRule): DataValidationRule | undefined {
45-
if (!rule) return rule;
46-
const locale = this.env.model.getters.getLocale();
47-
return localizeDataValidationRule(rule, locale);
48-
}
49-
5043
get validationRules() {
5144
const sheetId = this.env.model.getters.getActiveSheetId();
5245
return this.env.model.getters.getDataValidationRules(sheetId);

src/helpers/locale.ts

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import { tokenize } from "../formulas/tokenizer";
22
import { toNumber } from "../functions/helpers";
3-
import {
4-
ColorScaleThreshold,
5-
ConditionalFormatRule,
6-
DataValidationRule,
7-
DEFAULT_LOCALE,
8-
IconThreshold,
9-
Locale,
10-
} from "../types";
3+
import { DEFAULT_LOCALE, Locale } from "../types";
114
import { isDateTime } from "./dates";
125
import { formatValue, getDecimalNumberRegex } from "./format/format";
13-
import { deepCopy } from "./misc";
146
import { isNumber } from "./numbers";
157

168
export function isValidLocale(locale: any): locale is Locale {
@@ -232,85 +224,6 @@ function localizeLiteral(literal: string, locale: Locale): string {
232224
return localizeNumberLiteral(literal, locale);
233225
}
234226

235-
export function canonicalizeCFRule(
236-
cf: ConditionalFormatRule,
237-
locale: Locale
238-
): ConditionalFormatRule {
239-
return changeCFRuleLocale(cf, (content) => canonicalizeContent(content, locale));
240-
}
241-
242-
export function localizeCFRule(cf: ConditionalFormatRule, locale: Locale): ConditionalFormatRule {
243-
return changeCFRuleLocale(cf, (content) => localizeContent(content, locale));
244-
}
245-
246-
export function localizeDataValidationRule(
247-
rule: DataValidationRule,
248-
locale: Locale
249-
): DataValidationRule {
250-
const localizedDVRule = deepCopy(rule);
251-
localizedDVRule.criterion.values = localizedDVRule.criterion.values.map((content) =>
252-
localizeContent(content, locale)
253-
);
254-
return localizedDVRule;
255-
}
256-
257-
function changeCFRuleLocale(
258-
rule: ConditionalFormatRule,
259-
changeContentLocale: (content: string) => string
260-
): ConditionalFormatRule {
261-
rule = deepCopy(rule);
262-
switch (rule.type) {
263-
case "CellIsRule":
264-
// Only change value for number operators
265-
switch (rule.operator) {
266-
case "Between":
267-
case "NotBetween":
268-
case "Equal":
269-
case "NotEqual":
270-
case "GreaterThan":
271-
case "GreaterThanOrEqual":
272-
case "LessThan":
273-
case "LessThanOrEqual":
274-
rule.values = rule.values.map((v) => changeContentLocale(v));
275-
return rule;
276-
case "BeginsWith":
277-
case "ContainsText":
278-
case "EndsWith":
279-
case "NotContains":
280-
case "IsEmpty":
281-
case "IsNotEmpty":
282-
return rule;
283-
}
284-
case "DataBarRule":
285-
return rule;
286-
case "ColorScaleRule":
287-
rule.minimum = changeCFRuleThresholdLocale(rule.minimum, changeContentLocale);
288-
rule.maximum = changeCFRuleThresholdLocale(rule.maximum, changeContentLocale);
289-
if (rule.midpoint) {
290-
rule.midpoint = changeCFRuleThresholdLocale(rule.midpoint, changeContentLocale);
291-
}
292-
return rule;
293-
case "IconSetRule":
294-
rule.lowerInflectionPoint.value = changeContentLocale(rule.lowerInflectionPoint.value);
295-
rule.upperInflectionPoint.value = changeContentLocale(rule.upperInflectionPoint.value);
296-
return rule;
297-
}
298-
}
299-
300-
function changeCFRuleThresholdLocale<T extends IconThreshold | ColorScaleThreshold>(
301-
threshold: T,
302-
changeContentLocale: (content: string) => string
303-
): T {
304-
if (!threshold?.value) {
305-
return threshold;
306-
}
307-
308-
const value = threshold.type === "formula" ? "=" + threshold.value : threshold.value;
309-
const modified = changeContentLocale(value);
310-
const newValue = threshold.type === "formula" ? modified.slice(1) : modified;
311-
return { ...threshold, value: newValue };
312-
}
313-
314227
export function getDateTimeFormat(locale: Locale) {
315228
return locale.dateFormat + " " + locale.timeFormat;
316229
}

src/helpers/pivot/pivot_composer_helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { tokenColors } from "../../components/composer/composer/abstract_compose
22
import { CellComposerStore } from "../../components/composer/composer/cell_composer_store";
33
import { Token, getFunctionsFromTokens } from "../../formulas";
44
import { EnrichedToken } from "../../formulas/composer_tokenizer";
5-
import { Granularity, PivotField, PivotMeasure } from "../../types";
5+
import { Getters, Granularity, PivotField, PivotMeasure } from "../../types";
66

77
const PIVOT_FUNCTIONS = ["PIVOT.VALUE", "PIVOT.HEADER", "PIVOT"];
88

@@ -42,7 +42,7 @@ export function makeMeasureProposal(measure: PivotMeasure) {
4242
* Must be bound to the autocomplete provider.
4343
*/
4444
export function insertTokenAfterArgSeparator(
45-
this: { composer: CellComposerStore },
45+
this: { composer: CellComposerStore; getters: Getters },
4646
tokenAtCursor: EnrichedToken,
4747
value: string
4848
) {
@@ -64,7 +64,7 @@ export function insertTokenAfterArgSeparator(
6464
* @param {string} value
6565
*/
6666
export function insertTokenAfterLeftParenthesis(
67-
this: { composer: CellComposerStore },
67+
this: { composer: CellComposerStore; getters: Getters },
6868
tokenAtCursor: EnrichedToken,
6969
value: string
7070
) {

0 commit comments

Comments
 (0)