I would like to detect not only the client language (which I already do using a combination of different methods, from UA string extraction to geolocation services) but I would also like to detect the text direction in an automatic fashion, if possible.
I know there aren't too many languages using right-to-left direction (not as many as left-to-right ones, at least), so a possible solution would be to do something like $rtl = ['ar', 'he', ..., '<whatever>']; if (in_array(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2), $rtl)) { $direction = "rtl"; } else { $direction = "ltr"; }
but looks to me like there's (probably) a better solution.
I'm still studying some of the language recognition API's out there like LangID, AlchemyAPI and DetectLanguage, but they seem to do the same: recognize the text language, but not the text direction.
Any recommended approach?
The information about the direction of a language is accessible through the ICU library.
Using the cosmopolitan package it could be as simple as below.
<?php
require __DIR__. "/vendor/autoload.php";
use Salarmehr\Cosmopolitan\Cosmo;
echo Cosmo::create('fa')->direction(); // rlt
echo Cosmo::create('en')->direction(); // ltr