1

I've been struggling with this matter for two days, but still couldn't find anything useful.

Can someone advice me anything that will effectively detect a language in which a form was submitted, either any plugin or API, that I could use inside my php application.

The following methods would suit me well if they did not have limits on a number of requests you can make.

Google offers language detection library, which seems like it would suit me, but I'm completely unclear as to how to use it.

Are there any effective methods to detect language of user's input (short text) on the fly? Or maybe someone can help me figure out how to use Google Detection library with PHP?

(In my application different script is executed for each language, so I need to detect a language in which a form was submitted, in order to determine which script to execute)

qwaz
  • 1,285
  • 4
  • 23
  • 47
  • all what you need is written down here: https://developers.google.com/translate/v2/using_rest – Baalthasarr Jul 02 '14 at 10:51
  • 2
    You will hardly find a good language detection library for PHP. The Google one looks good, the Apache project also has one somewhere. Some Python NLA tools may offer something. The point being, you will pretty certainly have to do it in a language which is not PHP. How to best integrate that into what you have is quite a broad topic to answer sufficiently here. – deceze Jul 02 '14 at 10:51
  • On the other hand, you could always *ask* your user to provide you with that information before they submit the form, instead of trying to use complex magic. – N.B. Jul 02 '14 at 10:52
  • 1
    @Baalthasarr, I'm aware of that service, but it is a paid one, so it's not a viable option. – qwaz Jul 02 '14 at 10:57

1 Answers1

2

Use Text_LanguageDetect from Pear

Installation:

sudo pear install Text_LanguageDetect

Usage

Example:

<?php
require_once 'Text/LanguageDetect.php';
$l = new Text_LanguageDetect();

echo "Supported languages:\n";
try {
    $langs = $l->getLanguages();
    sort($langs);
    echo implode(', ', $langs) . "\n\n";
} catch (Text_LanguageDetect_Exception $e) {
    die($e->getMessage());
}

$text = <<<EOD
Hallo! Das ist ein Text in deutscher Sprache.
Mal sehen, ob die Klasse erkennt, welche Sprache das hier ist.
EOD;

try {
    //return 2-letter language codes only
    $l->setNameMode(2);

    $result = $l->detect($text, 4);
    print_r($result);
} catch (Text_LanguageDetect_Exception $e) {
    die($e->getMessage());
}
?>  

Output:

Supported languages:
albanian, arabic, azeri, bengali, bulgarian, cebuano, croatian, czech,
danish, dutch, english, estonian, farsi, finnish, french, german, hausa,
hawaiian, hindi, hungarian, icelandic, indonesian, italian, kazakh, kyrgyz,
latin, latvian, lithuanian, macedonian, mongolian, nepali, norwegian, pashto,
pidgin, polish, portuguese, romanian, russian, serbian, slovak, slovene, somali,
spanish, swahili, swedish, tagalog, turkish, ukrainian, urdu, uzbek, vietnamese,
welsh

Array
(
    [de] => 0.40703703703704
    [nl] => 0.2880658436214
    [en] => 0.28333333333333
    [da] => 0.23452674897119
)

Note: This package is not maintained. Read more


Another example of PHP language detector:

crodas/LanguageDetector

Ilia Ross
  • 13,086
  • 11
  • 53
  • 88