Skip to content

Commit

Permalink
feat(sheet): numfmt support i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
Gggpound committed Mar 18, 2024
1 parent 7fb6d35 commit 02cadaa
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import './index.less';

import { ICommandService, LocaleService, Range } from '@univerjs/core';
import numfmt from '@univerjs/engine-numfmt';
import { ITextSelectionRenderManager } from '@univerjs/engine-render';
import type { FormatType } from '@univerjs/sheets';
import { SelectionManagerService } from '@univerjs/sheets';
Expand All @@ -27,7 +26,7 @@ import React from 'react';
import { MENU_OPTIONS } from '../../base/const/MENU-OPTIONS';
import { SetNumfmtCommand } from '../../commands/commands/set-numfmt.command';
import { OpenNumfmtPanelOperator } from '../../commands/operations/open.numfmt.panel.operation';
import { getPatternType } from '../../utils/pattern';
import { getPatternPreview, getPatternType } from '../../utils/pattern';

export const MoreNumfmtType = (props: { value?: string }) => {
const localeService = useDependency(LocaleService);
Expand Down Expand Up @@ -88,7 +87,7 @@ export const Options = () => {
>
<div>{localeService.t(item.label)}</div>
<div className="m-l-26">
{item.pattern ? numfmt.format(item.pattern || '', defaultValue, { locale: 'zh-CN' }) : ''}
{item.pattern ? getPatternPreview(item.pattern || '', defaultValue, localeService.getCurrentLocale()).result : ''}
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
Disposable,
ICommandService,
LifecycleStages,
LocaleService,
ObjectMatrix,
OnLifecycle,
Range,
Expand All @@ -42,7 +43,8 @@ export class NumfmtCellContent extends Disposable {
@Inject(SheetSkeletonManagerService) private _sheetSkeletonManagerService: SheetSkeletonManagerService,
@Inject(ICommandService) private _commandService: ICommandService,

@Inject(INumfmtService) private _numfmtService: INumfmtService
@Inject(INumfmtService) private _numfmtService: INumfmtService,
@Inject(LocaleService) private _localeService: LocaleService
) {
super();
this._initInterceptorCellContent();
Expand Down Expand Up @@ -74,7 +76,7 @@ export class NumfmtCellContent extends Disposable {
return { ...cell, ...cache.result };
}

const info = getPatternPreview(numfmtValue.pattern, Number(originCellValue.v));
const info = getPatternPreview(numfmtValue.pattern, Number(originCellValue.v), this._localeService.getCurrentLocale());

numfmtRes = info.result;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export class NumfmtController extends Disposable implements INumfmtController {
) {
return next(cell);
}
const info = getPatternPreview(this._previewPattern, value as number);
const info = getPatternPreview(this._previewPattern, value as number, this._localeService.getCurrentLocale());
if (info.color) {
const colorMap = this._themeService.getCurrentTheme();
const color = colorMap[`${info.color}500`];
Expand Down
1 change: 1 addition & 0 deletions packages/sheets-numfmt/src/locale/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type zhCN from './zh-CN';
const locale: typeof zhCN = {
sheet: {
numfmt: {
percent: 'Percentage',
title: 'Number format',
numfmtType: 'Format types',
cancel: 'Cancel',
Expand Down
1 change: 1 addition & 0 deletions packages/sheets-numfmt/src/locale/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
const locale = {
sheet: {
numfmt: {
percent: '百分比',
title: '数字格式',
numfmtType: '格式类型',
cancel: '取消',
Expand Down
7 changes: 4 additions & 3 deletions packages/sheets-numfmt/src/utils/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

import numfmt from '@univerjs/engine-numfmt';
import type { FormatType } from '@univerjs/sheets';
import { LocaleType } from '@univerjs/core';

export const getPatternType = (pattern: string): FormatType => numfmt.getInfo(pattern).type || 'unknown';
export const getPatternPreview = (pattern: string, value: number) => {
export const getPatternPreview = (pattern: string, value: number, _locale?: LocaleType) => {
const info = numfmt.getInfo(pattern);

const locale = _locale === LocaleType.ZH_CN ? 'zh-CN' : 'en';
const negInfo = info._partitions[1];
const result = numfmt.format(pattern, value, { locale: 'zh-CN' });
const result = numfmt.format(pattern, value, { locale });
if (value < 0) {
return {
result,
Expand Down

0 comments on commit 02cadaa

Please sign in to comment.