2022-09-02 14:19:01 -04:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
namespace BookStack\Translation;
|
2022-09-02 14:19:01 -04:00
|
|
|
|
2023-09-02 10:11:42 -04:00
|
|
|
use BookStack\Users\Models\User;
|
2022-09-02 14:19:01 -04:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2023-09-17 08:29:06 -04:00
|
|
|
class LocaleManager
|
2022-09-02 14:19:01 -04:00
|
|
|
{
|
|
|
|
/**
|
2023-09-17 08:29:06 -04:00
|
|
|
* Array of right-to-left locale options.
|
2022-09-02 14:19:01 -04:00
|
|
|
*/
|
2023-09-17 08:29:06 -04:00
|
|
|
protected array $rtlLocales = ['ar', 'fa', 'he'];
|
2022-09-02 14:19:01 -04:00
|
|
|
|
|
|
|
/**
|
2023-09-17 17:02:12 -04:00
|
|
|
* Map of BookStack locale names to best-estimate ISO locale names.
|
2022-09-02 14:19:01 -04:00
|
|
|
* Locales can often be found by running `locale -a` on a linux system.
|
|
|
|
*
|
2023-09-17 17:02:12 -04:00
|
|
|
* @var array<string, string>
|
2022-09-02 14:19:01 -04:00
|
|
|
*/
|
|
|
|
protected array $localeMap = [
|
2023-09-17 17:02:12 -04:00
|
|
|
'ar' => 'ar',
|
|
|
|
'bg' => 'bg_BG',
|
|
|
|
'bs' => 'bs_BA',
|
|
|
|
'ca' => 'ca',
|
|
|
|
'cs' => 'cs_CZ',
|
|
|
|
'cy' => 'cy_GB',
|
|
|
|
'da' => 'da_DK',
|
|
|
|
'de' => 'de_DE',
|
|
|
|
'de_informal' => 'de_DE',
|
|
|
|
'el' => 'el_GR',
|
|
|
|
'en' => 'en_GB',
|
|
|
|
'es' => 'es_ES',
|
|
|
|
'es_AR' => 'es_AR',
|
|
|
|
'et' => 'et_EE',
|
|
|
|
'eu' => 'eu_ES',
|
|
|
|
'fa' => 'fa_IR',
|
|
|
|
'fr' => 'fr_FR',
|
|
|
|
'he' => 'he_IL',
|
|
|
|
'hr' => 'hr_HR',
|
|
|
|
'hu' => 'hu_HU',
|
|
|
|
'id' => 'id_ID',
|
|
|
|
'it' => 'it_IT',
|
|
|
|
'ja' => 'ja',
|
|
|
|
'ka' => 'ka_GE',
|
|
|
|
'ko' => 'ko_KR',
|
|
|
|
'lt' => 'lt_LT',
|
|
|
|
'lv' => 'lv_LV',
|
|
|
|
'nb' => 'nb_NO',
|
|
|
|
'nl' => 'nl_NL',
|
|
|
|
'pl' => 'pl_PL',
|
|
|
|
'pt' => 'pt_PT',
|
|
|
|
'pt_BR' => 'pt_BR',
|
|
|
|
'ro' => 'ro_RO',
|
|
|
|
'ru' => 'ru',
|
|
|
|
'sk' => 'sk_SK',
|
|
|
|
'sl' => 'sl_SI',
|
|
|
|
'sv' => 'sv_SE',
|
|
|
|
'tr' => 'tr_TR',
|
|
|
|
'uk' => 'uk_UA',
|
|
|
|
'uz' => 'uz_UZ',
|
|
|
|
'vi' => 'vi_VN',
|
|
|
|
'zh_CN' => 'zh_CN',
|
|
|
|
'zh_TW' => 'zh_TW',
|
2022-09-02 14:19:01 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2023-09-17 08:29:06 -04:00
|
|
|
* Get the BookStack locale string for the given user.
|
2022-09-02 14:19:01 -04:00
|
|
|
*/
|
2023-09-17 08:29:06 -04:00
|
|
|
protected function getLocaleForUser(User $user): string
|
2022-09-02 14:19:01 -04:00
|
|
|
{
|
2023-09-17 08:29:06 -04:00
|
|
|
$default = config('app.default_locale');
|
2022-09-02 14:19:01 -04:00
|
|
|
|
2023-09-16 08:18:35 -04:00
|
|
|
if ($user->isGuest() && config('app.auto_detect_locale')) {
|
2023-09-17 08:29:06 -04:00
|
|
|
return $this->autoDetectLocale(request(), $default);
|
2022-09-02 14:19:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return setting()->getUser($user, 'language', $default);
|
|
|
|
}
|
|
|
|
|
2023-09-02 10:11:42 -04:00
|
|
|
/**
|
2023-09-17 08:29:06 -04:00
|
|
|
* Get a locale definition for the current user.
|
2023-09-02 10:11:42 -04:00
|
|
|
*/
|
2023-09-17 08:29:06 -04:00
|
|
|
public function getForUser(User $user): LocaleDefinition
|
2023-09-02 10:11:42 -04:00
|
|
|
{
|
2023-09-17 08:29:06 -04:00
|
|
|
$localeString = $this->getLocaleForUser($user);
|
2023-09-02 10:11:42 -04:00
|
|
|
|
2023-09-17 08:29:06 -04:00
|
|
|
return new LocaleDefinition(
|
|
|
|
$localeString,
|
2023-09-17 17:02:12 -04:00
|
|
|
$this->localeMap[$localeString] ?? $localeString,
|
2023-09-17 08:29:06 -04:00
|
|
|
in_array($localeString, $this->rtlLocales),
|
|
|
|
);
|
2022-09-02 14:19:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Autodetect the visitors locale by matching locales in their headers
|
|
|
|
* against the locales supported by BookStack.
|
|
|
|
*/
|
|
|
|
protected function autoDetectLocale(Request $request, string $default): string
|
|
|
|
{
|
2023-09-17 11:20:21 -04:00
|
|
|
$availableLocales = $this->getAllAppLocales();
|
2023-09-17 08:29:06 -04:00
|
|
|
|
2022-09-02 14:19:01 -04:00
|
|
|
foreach ($request->getLanguages() as $lang) {
|
|
|
|
if (in_array($lang, $availableLocales)) {
|
|
|
|
return $lang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
2023-09-17 11:20:21 -04:00
|
|
|
/**
|
|
|
|
* Get all the available app-specific level locale strings.
|
|
|
|
*/
|
|
|
|
public function getAllAppLocales(): array
|
|
|
|
{
|
|
|
|
return array_keys($this->localeMap);
|
|
|
|
}
|
2022-09-05 08:18:37 -04:00
|
|
|
}
|