PHPackages                             landrok/language-detector - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. landrok/language-detector

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

landrok/language-detector
=========================

A fast and reliable PHP library for detecting languages

1.4.0(2y ago)134722.5k↓43.7%18[6 issues](https://github.com/landrok/language-detector/issues)2MITPHPPHP &gt;=7.4CI failing

Since Nov 2Pushed 2y ago7 watchersCompare

[ Source](https://github.com/landrok/language-detector)[ Packagist](https://packagist.org/packages/landrok/language-detector)[ Docs](https://github.com/landrok/language-detector)[ RSS](/packages/landrok-language-detector/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (8)Dependencies (2)Versions (10)Used By (2)

LanguageDetector
================

[](#languagedetector)

[![Build Status](https://camo.githubusercontent.com/eeaa838404c665b71396c6f83dcd7df455f68c578718141ec34a5ea31e2dc465/68747470733a2f2f7472617669732d63692e636f6d2f6c616e64726f6b2f6c616e67756167652d6465746563746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/landrok/language-detector)[![Test Coverage](https://camo.githubusercontent.com/dc7fd223524fe8d208aadd091fd2ae75bfe618a72a82f97cceb1a6ab25cf1a97/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6c616e64726f6b2f6c616e67756167652d6465746563746f722f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/landrok/language-detector/coverage)[![Code Climate](https://camo.githubusercontent.com/3e832123c334f4ebfd656ff6f5858b2928244536ddac525fa18f770be3b9df99/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6c616e64726f6b2f6c616e67756167652d6465746563746f722f6261646765732f6770612e737667)](https://codeclimate.com/github/landrok/language-detector)

LanguageDetector is a PHP library that detects the language from a text string.

Table of contents
=================

[](#table-of-contents)

- [Features](#features)
- [Install](#install)
- [Quick usage](#quick-usage)
    - [Detect language](#detect-language)
- [API Methods](#api-methods)
    - [evaluate()](#evaluate)
    - [getLanguage()](#getlanguage)
    - [getLanguages()](#getLanguages)
    - [getScores()](#getscores)
    - [getSupportedLanguages()](#getsupportedlanguages)
    - [getText()](#gettext)
    - [options](#options)
    - [For one-liners only](#for-one-liners-only)

Features
--------

[](#features)

- More than 50 supported languages, including Klingon
- Very fast, no database needed
- Packaged with a 2MB dataset
- Learning steps are already done, library is ready to use
- Small code, small footprint
- N-grams algorithm
- Supports PHP 5.4+, 7+ and 8+ and HHVM The latest release 1.4.x only supports PHP&gt;=7.4

Install
-------

[](#install)

```
composer require landrok/language-detector
```

---

Quick usage
-----------

[](#quick-usage)

### Detect language

[](#detect-language)

Instanciate a detector, pass a text and get the detected language.

```
require_once 'vendor/autoload.php';

$text = 'My tailor is rich and Alison is in the kitchen with Bob.';

$detector = new LanguageDetector\LanguageDetector();

$language = $detector->evaluate($text)->getLanguage();

echo $language; // Prints something like 'en'
```

Once it's instanciated, you can test multiple texts.

```
require_once 'vendor/autoload.php';

// An array of texts to evaluate
$texts = [
    'My tailor is rich and Alison is in the kitchen with Bob.',
    'Mon tailleur est riche et Alison est dans la cuisine avec Bob'
];

$detector = new LanguageDetector\LanguageDetector();

foreach ($texts as $key => $text) {

    $language = $detector->evaluate($text)->getLanguage();

    echo sprintf(
        "Text %d, language=%s\n",
        $key,
        $language
    );
}
```

Would output something like:

```
Text 0, language=en
Text 1, language=fr
```

Additionally, you can use a *LanguageDetector* instance as a string.

```
require_once 'vendor/autoload.php';

$text = 'My tailor is rich and Alison is in the kitchen with Bob.';

$detector = new LanguageDetector\LanguageDetector();

echo $detector->evaluate($text); // Prints something like 'en'
echo $detector; // Prints something like 'en' after an evaluate()
```

---

API Methods
-----------

[](#api-methods)

#### evaluate()

[](#evaluate)

**Type** *\\LanguageDetector\\LanguageDetector*

It performs an evaluation on a given text.

**Example**

After an `evaluate()`, the result is stored and available for later use.

```
$detector->evaluate('My tailor is rich and Alison is in the kitchen with Bob.');

// Then you have access to the detected language
$detector->getLanguage(); // Returns 'en'
```

You can make a one line call.

```
$detector->evaluate('My tailor is rich and Alison is in the kitchen with Bob.')
         ->getLanguage(); // Returns 'en'
```

It's possible to directly print `evaluate()` output.

```
// Returns 'en'
echo $detector->evaluate('My tailor is rich and Alison is in the kitchen with Bob.');
```

---

#### getLanguage()

[](#getlanguage)

**Type** *string*

The detected language

**Example**

```
$detector->getLanguage(); // Returns 'en'
```

---

#### getLanguages()

[](#getlanguages)

**Type** *array*

A list of loaded models that will be evaluated.

**Example**

```
$detector->getLanguages(); // Returns something like ['de', 'en', 'fr']
```

---

#### getScores()

[](#getscores)

**Type** *array*

A list of scores by language, for all evaluated languages.

**Example**

```
$detector->getScores();

// Returns something like
Array
(
    [en] => 0.43950135722745
    [nl] => 0.40898789832569
    [...]
    [ja] => 0
    [fa] => 0
)
```

---

#### getSupportedLanguages()

[](#getsupportedlanguages)

**Type** *array*

A list of supported languages that will be evaluated.

**Example**

```
$detector->getSupportedLanguages();

// Returns something like
Array
(
    [0] => af
    [1] => ar
    [...]
    [51] => zh-cn
    [52] => zh-tw

)
```

---

#### getText()

[](#gettext)

**Type** *string*

Returns the last string which has been evaluated

**Example**

```
$detector->getText();

// Returns 'My tailor is rich and Alison is in the kitchen with Bob.'
```

---

#### Options

[](#options)

**Type** *\\LanguageDetector\\LanguageDetector*

For even better performance, loaded models can be specified explicitly.

**Example**

```
$text = 'My tailor is rich and Alison is in the kitchen with Bob.';

$detector = new LanguageDetector(null, ['en', 'fr', 'de']);

$language = $detector->evaluate($text);

echo $language; // Prints something like 'en'
```

---

#### For one-liners only

[](#for-one-liners-only)

**Type** *\\LanguageDetector\\LanguageDetector*

With a static call on detect() method, you can perform an evaluation on a given text, in one line.

**Example**

```
echo LanguageDetector\LanguageDetector::detect(
    'My tailor is rich and Alison is in the kitchen with Bob.'
); // Returns 'en'
```

You can use all API methods.

```
$detector = LanguageDetector\LanguageDetector::detect(
    'My tailor is rich and Alison is in the kitchen with Bob.'
);

// en
echo $detector;

// en
echo $detector->getLanguage();

// An array of all scores, see API method
print_r($detector->getScores());

// An array of all supported languages, see API method
print_r($detector->getSupportedLanguages());

// The last evaluated string
echo $detector->getText();

// Limit loaded languages for even better performance
echo LanguageDetector\LanguageDetector::detect(
    'My tailor is rich and Alison is in the kitchen with Bob.',
    ['en', 'de', 'fr', 'es']
); // en
```

---

###  Health Score

45

—

FairBetter than 91% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity54

Moderate usage in the ecosystem

Community25

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 97.8% 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 ~325 days

Recently: every ~457 days

Total

9

Last Release

929d ago

PHP version history (5 changes)1.0.0PHP &gt;=5.4.0 || ^7.0

1.1.0PHP ^7.0

1.3.0PHP &gt;=7.0

1.3.1PHP &gt;=7.2

1.4.0PHP &gt;=7.4

### Community

Maintainers

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

---

Top Contributors

[![landrok](https://avatars.githubusercontent.com/u/3310446?v=4)](https://github.com/landrok "landrok (91 commits)")[![crishoj](https://avatars.githubusercontent.com/u/20393?v=4)](https://github.com/crishoj "crishoj (2 commits)")

---

Tags

language-detectorngramslanguagedetectorn-grams

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[symplify/monorepo-builder

Not only Composer tools to build a Monorepo.

5275.9M121](/packages/symplify-monorepo-builder)[phpactor/phpactor

PHP refactoring and intellisense tool for text editors

1.9k17.1k1](/packages/phpactor-phpactor)[phpdocumentor/reflection

Reflection library to do Static Analysis for PHP Projects

12525.9M149](/packages/phpdocumentor-reflection)[nitotm/efficient-language-detector

Fast and accurate natural language detection. Detector written in PHP. Nito-ELD, ELD.

63352.8k10](/packages/nitotm-efficient-language-detector)[sylius/promotion

Flexible promotion management for PHP applications.

28505.6k15](/packages/sylius-promotion)[sylius/money-bundle

Currencies and money formatting engine bundle for Symfony.

19681.3k26](/packages/sylius-money-bundle)

PHPackages © 2026

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