Skip to content

Commit

Permalink
feat(i18n): add support of the russian language (#2853)
Browse files Browse the repository at this point in the history
feat(i18n): add formatter support of the russian language
  • Loading branch information
corocoto authored and wjgogogo committed Aug 15, 2024
1 parent 145ea43 commit 3dcf491
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 15 deletions.
22 changes: 21 additions & 1 deletion packages/s2-core/__tests__/unit/common/i18n/index-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ describe('I18n Test', () => {
expect(getLang()).toEqual('zh_CN');
});

test('should set custom lang', () => {
test('should set english lang', () => {
setLang('en_US');
expect(getLang()).toEqual('en_US');
});

test('should set russian lang', () => {
setLang('ru');
expect(getLang()).toEqual('ru');
});

test('should get default chinese lang if receive a empty lang type', () => {
setLang(null as unknown as LangType);
expect(getLang()).toEqual('zh_CN');
Expand All @@ -30,6 +35,21 @@ describe('I18n Test', () => {
expect(i18n(',')).toEqual(', ');
});

test('should show russian text when set lang to ru_RU', () => {
setLang('ru_RU');
expect(i18n('小计')).toEqual('Промежуточный итог');
expect(i18n('总计')).toEqual('Общий итог');
expect(i18n('总和')).toEqual('(СУММА)');
expect(i18n('项')).toEqual('элемент(-а/-ов)');
expect(i18n('已选择')).toEqual('выбрано');
expect(i18n('序号')).toEqual('Индекс');
expect(i18n('度量')).toEqual('Мера');
expect(i18n('数值')).toEqual('Численная величина');
expect(i18n('共计')).toEqual('Общее количество');
expect(i18n('条')).toEqual('');
expect(i18n(',')).toEqual(', ');
});

test('should show Chinese text when set lang to zh', () => {
setLang('zh_CN');
expect(i18n('小计')).toEqual('小计');
Expand Down
22 changes: 18 additions & 4 deletions packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ describe('PivotSheet Tests', () => {
});

// https://github.com/antvis/S2/issues/1421
test.each(['zh_CN', 'en_US'] as LangType[])(
test.each(['zh_CN', 'en_US', 'ru_RU'] as LangType[])(
'should render group sort menu',
async (lang) => {
setLang(lang);
Expand All @@ -1026,9 +1026,23 @@ describe('PivotSheet Tests', () => {
sheet.handleGroupSort(event, null as unknown as Node);

const isEnUS = lang === 'en_US';
const groupAscText = isEnUS ? 'Group ASC' : '组内升序';
const groupDescText = isEnUS ? 'Group DESC' : '组内降序';
const groupNoneText = isEnUS ? 'No order' : '不排序';
const isRu = lang === 'ru_RU';

let groupAscText = '组内升序';
let groupDescText = '组内降序';
let groupNoneText = '不排序';

if (isEnUS) {
groupAscText = 'Group ASC';
groupDescText = 'Group DESC';
groupNoneText = 'No order';
}

if (isRu) {
groupAscText = 'Группировать по возрастанию';
groupDescText = 'Группировать по убыванию';
groupNoneText = 'Не отсортировано';
}

const options: TooltipOptions = {
onlyShowOperator: true,
Expand Down
22 changes: 18 additions & 4 deletions packages/s2-core/__tests__/unit/sheet-type/table-sheet-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('TableSheet Tests', () => {
});

// https://github.com/antvis/S2/issues/1421
test.each(['zh_CN', 'en_US'] as LangType[])(
test.each(['zh_CN', 'en_US', 'ru_RU'] as LangType[])(
'should render group sort menu',
async (lang) => {
setLang(lang);
Expand All @@ -184,9 +184,23 @@ describe('TableSheet Tests', () => {
sheet.handleGroupSort(event, null as unknown as Node);

const isEnUS = lang === 'en_US';
const groupAscText = isEnUS ? 'ASC' : '升序';
const groupDescText = isEnUS ? 'DESC' : '降序';
const groupNoneText = isEnUS ? 'No order' : '不排序';
const isRu = lang === 'ru_RU';

let groupAscText = '升序';
let groupDescText = '降序';
let groupNoneText = '不排序';

if (isEnUS) {
groupAscText = 'ASC';
groupDescText = 'DESC';
groupNoneText = 'No order';
}

if (isRu) {
groupAscText = 'По возрастанию';
groupDescText = 'По убыванию';
groupNoneText = 'Не отсортировано';
}

const options: TooltipOptions = {
onlyShowOperator: true,
Expand Down
4 changes: 3 additions & 1 deletion packages/s2-core/src/common/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { get, merge } from 'lodash';
import { EN_US as BASE_EN_US } from './en_US';
import { RU as BASE_RU } from './ru_RU';
import { ZH_CN as BASE_ZH_CN } from './zh_CN';

const DEFAULT_LANG: LangType = 'zh_CN';

export type LangType = 'zh_CN' | 'en_US';
export type LangType = 'zh_CN' | 'en_US' | 'ru_RU';

export type LocaleType = {
[K in LangType]: Record<string, string>;
Expand All @@ -15,6 +16,7 @@ let lang: LangType = DEFAULT_LANG;
let locale: LocaleType = {
zh_CN: BASE_ZH_CN,
en_US: BASE_EN_US,
ru_RU: BASE_RU,
};

export const getLang = () => lang;
Expand Down
20 changes: 20 additions & 0 deletions packages/s2-core/src/common/i18n/ru_RU.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const RU = {
小计: 'Промежуточный итог',
总计: 'Общий итог',
总和: '(СУММА)',
: 'элемент(-а/-ов)',
已选择: 'выбрано',
序号: 'Индекс',
度量: 'Мера',
数值: 'Численная величина',
共计: 'Общее количество',
: '',
隐藏: 'Скрыть',
趋势: 'Курс',
组内升序: 'Группировать по возрастанию',
组内降序: 'Группировать по убыванию',
升序: 'По возрастанию',
降序: 'По убыванию',
不排序: 'Не отсортировано',
',': ', ',
};
10 changes: 7 additions & 3 deletions packages/s2-core/src/utils/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { isNaN, toNumber } from 'lodash';
import { PRECISION } from '../common/constant';
import { getLang } from '../common/i18n';
import { getLang, type LangType } from '../common/i18n';

const FORMATTERS = {
const FORMATTERS: { [K in LangType]: [string[], number[]] } = {
en_US: ['KMBTP'.split(''), [1e3, 1e3, 1e3, 1e3, 1e3]],
ru_RU: [
['тысяча', 'миллион', 'миллиард', 'триллион', 'квадриллион'],
[1e3, 1e3, 1e3, 1e3, 1e3],
],
zh_CN: [
['万', '亿'],
[10000, 1e4],
[1e4, 1e4],
],
};

Expand Down
5 changes: 4 additions & 1 deletion packages/s2-react/src/components/config-provider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getLang, type ThemeName } from '@antv/s2';
import { ConfigProvider as AntdConfigProvider, theme } from 'antd';
import enUS from 'antd/es/locale/en_US';
import ruRU from 'antd/es/locale/ru_RU';
import zhCN from 'antd/es/locale/zh_CN';
import React from 'react';

Expand All @@ -11,7 +12,9 @@ export interface ConfigProviderProps {

export const ConfigProvider: React.FC<ConfigProviderProps> = (props) => {
const { children, themeName } = props;
const locale = getLang() === 'zh_CN' ? zhCN : enUS;
const lang = getLang();
// eslint-disable-next-line no-nested-ternary
const locale = lang === 'zh_CN' ? zhCN : lang === 'ru_RU' ? ruRU : enUS;
const isDarkTheme = themeName === 'dark';

return (
Expand Down
2 changes: 2 additions & 0 deletions packages/s2-shared/src/constant/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { LocaleType } from '@antv/s2';
import { EN_US } from './en_US';
import { RU } from './ru_RU';
import { ZH_CN } from './zh_CN';

export const Locale: LocaleType = {
zh_CN: ZH_CN,
en_US: EN_US,
ru_RU: RU,
};
47 changes: 47 additions & 0 deletions packages/s2-shared/src/constant/i18n/ru_RU.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export const RU: Record<string, string> = {
选择分析信息: 'Выберите информацию для анализа',
切换指标: 'Индикатор переключения',
确定: 'Подтвердить',
取消: 'Отмена',
重置: 'Сбросить',
请输入关键字搜索: 'Пожалуйста, введите поиск по ключевым словам',
搜索: 'Поиск',
恢复: 'Восстановить',
'已选 {} 项': 'Выбран(-но) {} элемент(-а/-ов)',
所选项: 'выбранные элементы',
选择下钻维度: 'Выберите параметры детализации',
恢复默认: 'Сбросить до первоначального состояния',
搜索字段: 'Поисковое поле',

// switcher
行列切换: 'Конфигурация отображения строк, столбцов и значений',
行头: 'Строки',
列头: 'Колонки',
: 'Значения',
展开子项: 'Отобразить дочерние элементы',

// sort
首字母: 'Алфавитная сортировка',
手动排序: 'Ручная сортировка',
其他字段: 'Другое поле',
高级排序: 'Расширенная сортировка',
设置顺序: 'Установить сортировку',
可选字段: 'Необязательные поля',
'按以下规则进行排序(优先级由低到高)':
'Порядок, определеяемый следующими правилами (от низкого приоритета к высокому)',
: 'По',
说明: 'Описание: ',

// export
复制原始数据: 'Копировать строковые данные',
复制格式化数据: 'Копировать отформатированные данные',
下载原始数据: 'Скачать строковые данные',
下载格式化数据: 'Скачать отформатированные данные',
操作成功: 'Операция успешно выполнена',
操作失败: 'Операция завершилась неудачно',

// strategy
时间: 'Дата',
指标: 'Мера',
目标值: 'Целевое Значение',
};
8 changes: 7 additions & 1 deletion s2-site/docs/manual/basic/i18n.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ order: 8

`S2` currently has built-`中/英`two sets of copywriting, the default is **Chinese** , [view all files](https://github.com/antvis/S2/tree/next/packages/s2-core/src/common/i18n)

## switch to english
## switch to selected language

Switch the international text by `setLang` , note: it needs to be called before the table is instantiated

*NB. Right now you only can use `en_US`, `zh_CN`, and `ru` built-in locales.*

```ts
import { setLang, PivotSheet } from '@antv/s2'

setLang('en_US')
// setLang('zh_CN')
// setLang('ru_RU');

const s2 = new PivotSheet()
s2.render()
Expand All @@ -33,6 +36,9 @@ const locale = {
en_US: {
test: 'test',
},
ru_RU: {
test: 'тест',
},
};

extendLocale(locale)
Expand Down
4 changes: 4 additions & 0 deletions s2-site/docs/manual/basic/i18n.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { setLang, PivotSheet } from '@antv/s2'

setLang('en_US')
// setLang('zh_CN')
// setLang('ru_RU');

const s2 = new PivotSheet(...)
await s2.render()
Expand All @@ -33,6 +34,9 @@ const locale = {
en_US: {
test: 'test',
},
ru_RU: {
test: 'тест',
},
};

extendLocale(locale)
Expand Down

0 comments on commit 3dcf491

Please sign in to comment.