aozen / language-detector
This package provides a set of functions to detect and manipulate language-specific strings, including language detection, case conversion, and language-based filtering.
Requires (Dev)
- phpunit/phpunit: ^9.6
This package is auto-updated.
Last update: 2024-06-20 02:01:23 UTC
README
Language Detector
Used to determine which language a given string belongs to.
Installation
You can install the package via composer:
composer require aozen/language-detector
Usage
The LanguageDetector class must be included by yourself.
use \Aozen\LanguageDetector\LanguageDetector;
Example:
class YourClass { public function yourFunction() { $language_detector = new LanguageDetector(); return $language_detector->detect("Güneş")->getLanguage(); // Result: "tr" } }
Other Usage Examples
-
By default, the code searches in all languages specified as
available
within the code. -
The
getClosestLanguage
function works similarly togetLanguage
, but it doesn't guarantee a 100% correct result. For instance, the exampleÄpfel straße délicieux
would returninvalid_language
withgetLanguage
, whilegetClosestLanguage
would returnde
, based on which language had the most character matches. For instance, the expressiondélicieux délicieux Äpfel
would have also returnedfr
. -
To search only in the desired languages,
checkList
is used.
$language_detector->detect("Äpfel")->checkList("tr", "de", "it")->getLanguage(); // de
- To prevent searching in unwanted languages,
blockList
is used.
$language_detector->detect("Äpfel")->blockList("de")->getLanguage(); // invalid_language
- To retrieve the
lowercase
version of the given string for each language,getLowerCase
is used. Separate definitions for each language are required.
$language_detector->detect("TÜRKÇE-BİR-YAZI")->getLowerCase(); // türkçe-bi̇r-yazı
Testing
vendor/bin/phpunit tests/Test.php