/** * Classify a file or the standard input */ function classify($argv) { if (!isset($argv[2])) { throw new RuntimeException("classify command requires at least one argument\n{$argv[0]} classify [file or - for stdin] [input_model default is 'model']"); } $input_model = isset($argv[3]) ? $argv[3] : "model"; $lang_detect = LanguageDetector::loadFromFile($input_model); if ($argv[2] == "-") { $language = $lang_detect->classify(file_get_contents("php://stdin")); } else { $language = $lang_detect->classifyFile($argv[2]); } echo $language, PHP_EOL; }
/** * Get a user's language and locale. * * @param string $separator * * @return string */ public function getLanguageLocale($separator = '-') { if (!is_array($this->languages)) { LanguageDetector::detect($this, $this->getAcceptLanguage()); } $userLanguage = $this->getLanguage(); foreach ($this->languages as $language) { if (strlen($language) === 5 && strpos($language, $userLanguage) === 0) { $locale = substr($language, -2); break; } } if (!empty($locale)) { return $userLanguage . $separator . strtoupper($locale); } else { return $userLanguage; } }