PHPackages                             thingston/language - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. thingston/language

ActiveLibrary[Localization &amp; i18n](/categories/localization)

thingston/language
==================

Pure-PHP language detection library using n-gram profiles

1.0.1(1mo ago)02MITPHPPHP ^8.2

Since Jun 19Pushed 1mo agoCompare

[ Source](https://github.com/thingston/language)[ Packagist](https://packagist.org/packages/thingston/language)[ RSS](/packages/thingston-language/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (3)Versions (3)Used By (0)

Thingston Language
==================

[](#thingston-language)

Pure-PHP language detection library using n-gram frequency profiles. Given any input text, returns a ranked list of candidate languages with confidence scores. No external services or compiled extensions required beyond `ext-mbstring`.

Installation
------------

[](#installation)

```
composer require thingston/language
```

Usage
-----

[](#usage)

```
use Thingston\Language\LanguageDetector;

$detector = new LanguageDetector();

$results = $detector->detect('Bonjour tout le monde');

$best = $results->best();
echo $best->getCode();       // fr
echo $best->getName();       // French
echo $best->getConfidence(); // ~0.97

foreach ($results->top(5) as $score) {
    echo $score->getCode() . ': ' . round($score->getConfidence() * 100, 1) . '%' . PHP_EOL;
}
```

### Restrict to a subset of languages

[](#restrict-to-a-subset-of-languages)

```
$detector = new LanguageDetector(languages: ['en', 'fr', 'de', 'es']);
$best = $detector->detect('The weather is lovely today')->best();
echo $best->getCode(); // en
```

### Custom n-gram sizes

[](#custom-n-gram-sizes)

```
$detector = new LanguageDetector(ngramSizes: [1, 2, 3]);
```

### Custom profile repository

[](#custom-profile-repository)

```
use Thingston\Language\Profile\ProfileRepository;

$detector = new LanguageDetector(profileRepository: new ProfileRepository('/path/to/profiles'));
```

Supported languages (74)
------------------------

[](#supported-languages-74)

CodeLanguageCodeLanguageCodeLanguageafAfrikaanshiHindipsPashtoamAmharichrCroatianptPortuguesearArabichuHungarianroRomanianazAzerbaijaniidIndonesianruRussianbeBelarusianigIgbosiSinhalabgBulgarianitItalianskSlovakbnBengalijaJapaneseslSlovenianbsBosniankkKazakhsoSomalicaCatalankmKhmersqAlbaniancsCzechknKannadasrSerbiancyWelshkoKoreansvSwedishdaDanishloLaoswSwahilideGermanltLithuaniantaTamilelGreeklvLatvianteTeluguenEnglishmkMacedoniantgTajikesSpanishmlMalayalamthThaietEstonianmnMongoliantlTagalogeuBasquemrMarathitrTurkishfaPersianmsMalayukUkrainianfiFinnishmyBurmeseurUrdufrFrenchneNepaliuzUzbekglGaliciannlDutchviVietnameseguGujaratinoNorwegianyoYorubahaHausapaPunjabizhChineseheHebrewplPolishHow it works
------------

[](#how-it-works)

Language profiles are built from [Tatoeba](https://tatoeba.org) sentence corpora (CC-BY 2.0). For each language, the top 1,000 most frequent character n-grams (sizes 1–4) are stored as relative frequencies.

At detection time the input text is normalized (lowercased, non-letter characters stripped), n-grams are extracted, and each language profile is scored with a log-probability sum. Raw scores are converted to confidences via softmax.

Rebuilding profiles
-------------------

[](#rebuilding-profiles)

If you want to regenerate language profiles from fresh training data:

```
# 1. Download Tatoeba sentence corpora (requires ext-bzip2)
php bin/download-corpus.php

# 2. Build PHP profile files
php bin/build-profiles.php

# Optional: restrict to specific languages
php bin/download-corpus.php en fr de
php bin/build-profiles.php en fr de --top=1000 --sizes=1,2,3,4
```

Development
-----------

[](#development)

```
composer install

# Run unit tests
composer test-unit

# Run accuracy tests (requires built profiles)
composer test-accuracy

# Code style check
composer cs

# Code style fix
composer cs-fix

# Static analysis (PHPStan level 9)
composer stan
```

License
-------

[](#license)

MIT

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance93

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~9 days

Total

2

Last Release

36d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/04e91a806958f09ca8b3f4c24c6f5c4bcbce3c4e44dd882fa1e4c737ee7774c6?d=identicon)[pedrobrazao](/maintainers/pedrobrazao)

---

Top Contributors

[![pedrobrazao](https://avatars.githubusercontent.com/u/772193?v=4)](https://github.com/pedrobrazao "pedrobrazao (2 commits)")

---

Tags

i18nlanguagelocalenlpnatural language processingtext classificationlanguage detectionn-gramngram

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/thingston-language/health.svg)

```
[![Health](https://phpackages.com/badges/thingston-language/health.svg)](https://phpackages.com/packages/thingston-language)
```

###  Alternatives

[aplus/language

Aplus Framework Language Library

2431.7M15](/packages/aplus-language)[vluzrmos/language-detector

Detect the language for your application using browser preferences, subdomains or route prefixes.

109577.5k3](/packages/vluzrmos-language-detector)[tractorcow/silverstripe-fluent

Simple localisation for Silverstripe

91442.7k30](/packages/tractorcow-silverstripe-fluent)[fisharebest/localization

A lightweight localization database and translation tools, with data from the CLDR, IANA, ISO, etc.

29116.6k3](/packages/fisharebest-localization)[sylius/locale

Locale handling for PHP applications.

10572.2k31](/packages/sylius-locale)[delight-im/i18n

Internationalization and localization for PHP

595.5k3](/packages/delight-im-i18n)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
