Skip to content

方言支持

relationship-ts 支持多种地区方言模式。

内置方言

方言导入路径说明
简体中文(默认)relationship-ts标准普通话称谓
繁体中文relationship-ts/zh-HK繁体字称谓
粤语(广东)relationship-ts/locale/guangdong粤语惯用称谓
北方方言relationship-ts/locale/north北方地区称谓

使用方言模式

粤语(广东)方言

ts
import relationship from 'relationship-ts';
import { guangdong } from 'relationship-ts/locale/guangdong';

// 设置方言模式
relationship.setMode('guangdong', guangdong.data);

// 使用方言模式查询
relationship({ text: '妈妈的妈妈', mode: 'guangdong' });
// => ['阿婆']

relationship({ text: '爸爸的爸爸', mode: 'guangdong' });
// => ['阿公']

北方方言

ts
import relationship from 'relationship-ts';
import north from 'relationship-ts/locale/north';

// 设置方言模式
relationship.setMode('north', north.data);

// 使用方言模式查询
relationship({ text: '妈妈的妈妈', mode: 'north' });
// => ['姥姥']

relationship({ text: '爸爸的爸爸', mode: 'north' });
// => ['姥爷']

繁体中文版本

ts
// 繁体中文版本是一个独立的构建
import relationship from 'relationship-ts/zh-HK';

relationship({ text: '爸爸的媽媽' });
// => ['奶奶', '祖母']

自定义方言

你可以创建自己的方言模式:

ts
import relationship from 'relationship-ts';

// 定义自定义方言
const myDialect = {
  'f': ['老豆'],
  'm': ['妈咪'],
  'f,f': ['阿公'],
  'f,m': ['阿嫲'],
  'm,f': ['外公'],
  'm,m': ['外婆'],
};

// 设置自定义方言
relationship.setMode('my-dialect', myDialect);

// 使用自定义方言
relationship({ text: '爸爸', mode: 'my-dialect' });
// => ['老豆']

方言数据格式

方言数据是一个对象,键是关系链,值是对应的称谓数组:

ts
interface TitleData {
  [chain: string]: string[];
}

// 示例
const data: TitleData = {
  'f': ['爸爸', '父亲', '老爸'],
  'm': ['妈妈', '母亲', '老妈'],
  'f,f': ['爷爷', '祖父', '阿公'],
  'f,m': ['奶奶', '祖母', '阿嫲'],
};

下一步

基于 MIT 许可发布