Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if string is persian or english

I have a webview which will load a string from a url, I'm not sure if this is the correct way or not but what I want to do is to check if the string is in persian so I change my webview's text alignment to rtl and else if it's in english change it to ltr. Is it possible to determine if the string is in persian or english? or if there's any other better way to handle this matter ?

Thanks in advance.

like image 843
arash moeen Avatar asked Dec 11 '22 04:12

arash moeen


2 Answers

Try the following regular expression, to check the Arabic, Persian and Hebrew characters range.

public static final Pattern RTL_CHARACTERS = 
    Pattern.compile("[\u0600-\u06FF\u0750-\u077F\u0590-\u05FF\uFE70-\uFEFF]");
Matcher matcher = RTL_CHARACTERS.matcher("براي تست");
if(matcher.find()){
   return true;  // it's RTL
} 
like image 117
VahidN Avatar answered Dec 22 '22 07:12

VahidN


Try Persian-tools an awesome javascript library for this matter and also many other useful functionalities.

import { isPersian, toPersianChars } from "persian-tools2";

isPersian("این یک متن فارسی است؟") // true
isPersian("Lorem Ipsum Test") // false
like image 43
Majid Shahabfar Avatar answered Dec 22 '22 07:12

Majid Shahabfar