siugnur/whatlang-php

Natural language detection library for PHP

Maintainers

Package info

github.com/SIugnur/whatlang-php

pkg:composer/siugnur/whatlang-php

Transparency log

Statistics

Installs: 14

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.1.0 2026-07-16 08:54 UTC

This package is auto-updated.

Last update: 2026-07-16 09:31:19 UTC


README

基于 whatlang-rs 开发的 PHP 语言检测库,支持检测 70 种语言26 种书写系统

📖 中文文档 | 📖 English Documentation

安装

composer require siugnur/whatlang-php

基础用法

检测语言和书写系统

use Whatlang\Detector;
use Whatlang\Lang;
use Whatlang\Script;

$detector = Detector::new();

$info = $detector->detect("Hello World");

echo $info->lang();       // Lang::Eng
echo $info->script();     // Script::Latin
echo $info->confidence(); // 1.0
echo $info->isReliable(); // true

只检测语言

$detector = Detector::new();

$lang = $detector->detectLang("Bonjour le monde");
echo $lang; // Lang::Fra

只检测书写系统

$detector = Detector::new();

$script = $detector->detectScript("Привет мир");
echo $script; // Script::Cyrillic

使用便捷函数

use function Whatlang\{detect, detectLang, detectScript};

$info = detect("こんにちは世界");
echo $info->lang(); // Lang::Jpn

$lang = detectLang("你好世界");
echo $lang; // Lang::Cmn

语言过滤

只检测指定语言(白名单)

$detector = Detector::withAllowlist([Lang::Eng, Lang::Spa, Lang::Fra]);

$lang = $detector->detectLang("Hello World");
// 如果白名单包含 Eng,返回 Eng,否则返回 null

排除指定语言(黑名单)

$detector = Detector::withDenylist([Lang::Jpn, Lang::Kor]);

$lang = $detector->detectLang("こんにちは");
echo $lang; // null (日文被排除)

语言信息

// 获取语言属性
Lang::Eng->code();    // "eng"
Lang::Eng->name();     // "English"
Lang::Eng->engName();  // "English"

// 通过代码获取语言
Lang::fromCode("eng");  // Lang::Eng
Lang::fromCode("zho");  // Lang::Cmn

支持的语言

语言 代码 语言 代码
中文 zho 英语 eng
俄语 rus 西班牙语 spa
法语 fra 德语 deu
日语 jpn 韩语 kor
阿拉伯语 ara 印地语 hin
葡萄牙语 por 意大利语 ita
越南语 vie 土耳其语 tur
荷兰语 nld 波兰语 pol
希腊语 ell 希伯来语 heb
波斯语 fas 泰语 tha
孟加拉语 ben 乌克兰语 ukr

查看全部 70 种语言...

运行测试

composer install
./vendor/bin/phpunit

License

MIT