<p>Depending on the case, you might be interested in using one of the following methods:</p>

<p><strong>Method 0: Use an API or library</strong></p>

<ul>
<li><a href="https://pypi.org/project/cld2-cffi/" rel="nofollow noreferrer">cld2-cffi</a></li>
<li><a href="https://cloud.google.com/translate/docs/basic/detecting-language" rel="nofollow noreferrer">Google Cloud Translation - Basic (v2)</a></li>
<li><a href="https://textblob.readthedocs.io/en/dev/" rel="nofollow noreferrer">TextBlob</a></li>
<li><a href="https://github.com/Mimino666/langdetect" rel="nofollow noreferrer">langdetect</a></li>
<li>etc.</li>
</ul>

<p>Usually, there are a few problems with these libraries because some of them are not accurate for small texts, some languages are missing, are slow, require internet connection, are non-free,... But generally speaking, they will suit most needs.</p>

<p><strong>Method 1: Language models</strong></p>

<p>A language model gives us the probability of a sequence of words. This is important because it allows us to robustly detect the language of a text, even when the text contains words in other languages (e.g.: <em>"'Hola' means 'hello' in spanish"</em>).</p>

<p>You can use N language models (one per language), to score your text. The detected language will be the language of the model that gave you the highest score.</p>

<p>If you want to build a simple language model for this, I'd go for 1-grams. To do this, you only need to count the number of times each word from a big text (e.g. Wikipedia Corpus in "X" language) has appeared.</p>

<p>Then, the probability of a word will be its frequency divided by the total number of words analyzed (sum of all frequencies).</p>

<pre><code>the 23135851162
of  13151942776
and 12997637966
to  12136980858
a   9081174698
in  8469404971
for 5933321709
...

=&gt; P("'Hola' means 'hello' in spanish") = P("hola") * P("means") * P("hello") * P("in") * P("spanish")
</code></pre>

<p>If the text to detect is quite big, I recommend sampling N random words and then use the sum of logarithms instead of multiplications to avoid floating-point precision problems.</p>

<pre><code>P(s) = 0.03 * 0.01 * 0.014 = 0.0000042
P(s) = log10(0.03) + log10(0.01) + log10(0.014) = -5.376
</code></pre>

<p><strong>Method 2: Intersecting sets</strong></p>

<p>An even simpler approach is to prepare N sets (one per language) with the top M most frequent words. Then intersect your text with each set. The set with the highest number of intersections will be your detected language.</p>

<pre><code>spanish_set = {"de", "hola", "la", "casa",...}
english_set = {"of", "hello", "the", "house",...}
czech_set = {"z", "ahoj", "závěrky", "dům",...}
...

text_set = {"hola", "means", "hello", "in", "spanish"}

spanish_votes = text_set.intersection(spanish_set)  # 1
english_votes = text_set.intersection(english_set)  # 4
czech_votes = text_set.intersection(czech_set)  # 0
...
</code></pre>

<p><strong>Method 3: Zip compression</strong></p>

<p>This more a curiosity than anything else, but here it goes... You can compress your text (e.g LZ77) and then measure the zip-distance with regards to a reference compressed text (target language). Personally, I didn't like it because it's slower, less accurate and less descriptive than other methods. Nevertheless, there might be interesting applications for this method. 
To read more: <a href="https://arxiv.org/abs/cond-mat/0108530" rel="nofollow noreferrer">Language Trees and Zipping</a></p>
How to determine the language of a piece of text?
<p>I want to get this:</p>
<pre class="lang-none prettyprint-override"><code>Input text: &quot;ру́сский язы́к&quot;
Output text: &quot;Russian&quot; 

Input text: &quot;中文&quot;
Output text: &quot;Chinese&quot; 

Input text: &quot;にほんご&quot;
Output text: &quot;Japanese&quot; 

Input text: &quot;العَرَبِيَّة&quot;
Output text: &quot;Arabic&quot;
</code></pre>
<p>How can I do it in python?</p>