icu4j cyrillic to latin

六月ゝ 毕业季﹏ 提交于 2019-11-27 04:28:34

问题


I'm trying to get Cyrillic words to be in latin so I can have them in urls. I use icu4j transliterator, but it still gives weird characters like this: Vilʹândimaa. It should be more like viljandimaa. When I copy that url these letters turn to %.. something useless.

Does anybody know how to get Cyrillic to a-z with icu4j?

UPDATE

Can't answer myself already but found this question that was very helpful: Converting Symbols, Accent Letters to English Alphabet


回答1:


Modify your identifier to do what you want. You can strip unwanted characters using a regular expression with the Remove transform.

For example, consider the string "'Eé математика":

"'E\u00E9 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0430"

The identifier "Any-Latin; NFD; [^\\p{Alnum}] Remove" will transliterate to Latin (which may still include accents), decompose accented characters into the letter and diacritics and remove anything that isn't an alphanumeric. The resultant string is "Eematematika".

You can read more on the identifiers under General Transforms on the ICU website.


Example:

//import com.ibm.icu.text.Transliterator;
String greek
       = "'E\u00E9 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0430";
String id = "Any-Latin; NFD; [^\\p{Alnum}] Remove";
String latin = Transliterator.getInstance(id)
                             .transform(greek);
System.out.println(latin);

Tested against ICU4J 49.1.




回答2:


Have a look at: https://ru.stackoverflow.com/questions/633355/Показать-правильный-пример-транслитерации-на-java

Add denepdency:

<dependency>
    <groupId>com.ibm.icu</groupId>
    <artifactId>icu4j</artifactId>
    <version>63.1</version>
</dependency>

And transliterate:

var CYRILLIC_TO_LATIN = "Latin-Russian/BGN"
// var CYRILLIC_TO_LATIN = "Russian-Latin/BGN"
Transliterator toLatinTrans = Transliterator.getInstance(CYRILLIC_TO_LATIN);
String result = toLatinTrans.transliterate(st);
System.out.println(result);



回答3:


Have no idea about icu4j, but in the Unicode table Cyrillic takes only a small range. Instead of relaying onto third-party libraries with unclear workings, I'd define a transliteration sequence for each Cyrillic symbol and did the translation myself.

P.S. What language word "viljandimaa" comes from? Doesn't sound like Cyrillic to me...



来源:https://stackoverflow.com/questions/5818912/icu4j-cyrillic-to-latin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!