How to detect language of text?


I have a form which lets users input text snippets. So how can figure out the language of the entered text?

Specifically these languages for now:

Arabic: ??? ?? ??? ?????? ???????

Chinese: 这是一些阿拉伯文字

Japanese: これは、いくつかのアラビア语のテキストです

[Edit] The detection has work on text which is retrieved via an API too (no browser involved)