目录

How do I remove the phonetic mark when converting Chinese characters to Pinyin?

You can use Any-Latn to convert Chinese characters into Pinyin, and then use Latin-ASCII to remove phonetic symbols. The sample code snippet is as follows:

import { i18n } from '@kit.LocalizationKit';

@Entry
@Component
struct RemovePinyin {
  @State message: string = 'Hello World';

  build() {
    RelativeContainer() {
      Text(this.message)
        .fontSize(50)
        .onClick(() => {
          // 1. Chinese characters to Pinyin
          let transliterator1 = i18n.Transliterator.getInstance('Any-Latn');
          let res = transliterator1.transform('China');
          // 2. Remove tone marks
          let transliterator2 = i18n.Transliterator.getInstance('Latin-ASCII');
          let pinyinResult = transliterator2.transform(res);
          console.info('pinyinResult: ' + pinyinResult);
        })
    }
    .height('100%')
    .width('100%')
  }
}