iconv - 尽可能音译,否则不转换
iconv - Transliterate if possible, otherwise leave unconverted
考虑以下示例文本行:
α Arietis, called Hamal, is the brightest star in Aries. Its traditional name is derived from the Arabic word for “lamb” or “head of the ram” (ras al-hamal).
它有三个不同的 UTF-8 字符,α
,一个左智能引号,一个右智能引号。
我的目标是尽可能将 UTF-8 音译为常规 ASCII,但保留所有不可转换的字符。 (在上面的示例文本中,引号可以音译为 "
,但 α
不能。)
我当前的命令是:
iconv -f UTF-8 -t ASCII//TRANSLIT < iconv.sample
但是,它无法转换 α
并以 iconv: (stdin):1:0: cannot convert
终止。
如果我将 //IGNORE
添加到目标或使用 -c
选项,它会完全删除 α
。
如果可以,我如何进行音译,如果不能,则回退到原始输入字符?
我不确定使用 iconv
时是否可行,因为输出编码必须符合(也就是说,如果您指定 ASCII
,它只会吐出 ASCII
, 不管怎样).
如果您有 uconv 可用,您可以指定远离输出编码的音译操作:
uconv -f "UTF-8" -t "UTF-8" -x "Latin-ASCII"
举个例子:
$ echo "α Arietis “head of the ram”" | uconv -f "UTF-8" -t "UTF-8" -x "Latin-ASCII"
α Arietis "head of the ram"
考虑以下示例文本行:
α Arietis, called Hamal, is the brightest star in Aries. Its traditional name is derived from the Arabic word for “lamb” or “head of the ram” (ras al-hamal).
它有三个不同的 UTF-8 字符,α
,一个左智能引号,一个右智能引号。
我的目标是尽可能将 UTF-8 音译为常规 ASCII,但保留所有不可转换的字符。 (在上面的示例文本中,引号可以音译为 "
,但 α
不能。)
我当前的命令是:
iconv -f UTF-8 -t ASCII//TRANSLIT < iconv.sample
但是,它无法转换 α
并以 iconv: (stdin):1:0: cannot convert
终止。
如果我将 //IGNORE
添加到目标或使用 -c
选项,它会完全删除 α
。
如果可以,我如何进行音译,如果不能,则回退到原始输入字符?
我不确定使用 iconv
时是否可行,因为输出编码必须符合(也就是说,如果您指定 ASCII
,它只会吐出 ASCII
, 不管怎样).
如果您有 uconv 可用,您可以指定远离输出编码的音译操作:
uconv -f "UTF-8" -t "UTF-8" -x "Latin-ASCII"
举个例子:
$ echo "α Arietis “head of the ram”" | uconv -f "UTF-8" -t "UTF-8" -x "Latin-ASCII"
α Arietis "head of the ram"