2022-09-02 20:19:01 +02:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 18:56:55 +02:00
|
|
|
namespace BookStack\Translation;
|
2022-09-02 20:19:01 +02:00
|
|
|
|
2023-09-02 16:11:42 +02:00
|
|
|
use BookStack\Users\Models\User;
|
2022-09-02 20:19:01 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2023-09-17 14:29:06 +02:00
|
|
|
class LocaleManager
|
2022-09-02 20:19:01 +02:00
|
|
|
{
|
|
|
|
/**
|
2023-09-17 14:29:06 +02:00
|
|
|
* Array of right-to-left locale options.
|
2022-09-02 20:19:01 +02:00
|
|
|
*/
|
2023-09-17 14:29:06 +02:00
|
|
|
protected array $rtlLocales = ['ar', 'fa', 'he'];
|
2022-09-02 20:19:01 +02:00
|
|
|
|
|
|
|
/**
|
2023-09-17 23:02:12 +02:00
|
|
|
* Map of BookStack locale names to best-estimate ISO locale names.
|
2022-09-02 20:19:01 +02:00
|
|
|
* Locales can often be found by running `locale -a` on a linux system.
|
|
|
|
*
|
2023-09-17 23:02:12 +02:00
|
|
|
* @var array<string, string>
|
2022-09-02 20:19:01 +02:00
|
|
|
*/
|
|
|
|
protected array $localeMap = [
|
2023-09-17 23:02:12 +02: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',
|
2023-10-30 12:41:36 +01:00
|
|
|
'fi' => 'fi_FI',
|
2023-09-17 23:02:12 +02:00
|
|
|
'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',
|
2023-10-30 12:41:36 +01:00
|
|
|
'nn' => 'nn_NO',
|
2023-09-17 23:02:12 +02:00
|
|
|
'pl' => 'pl_PL',
|
|
|
|
'pt' => 'pt_PT',
|
|
|
|
'pt_BR' => 'pt_BR',
|
|
|
|
'ro' => 'ro_RO',
|
|
|
|
'ru' => 'ru',
|
|
|
|
'sk' => 'sk_SK',
|
|
|
|
'sl' => 'sl_SI',
|
2023-10-30 12:41:36 +01:00
|
|
|
'sq' => 'sq_AL',
|
2023-09-17 23:02:12 +02:00
|
|
|
'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 20:19:01 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2023-09-17 14:29:06 +02:00
|
|
|
* Get the BookStack locale string for the given user.
|
2022-09-02 20:19:01 +02:00
|
|
|
*/
|
2023-09-17 14:29:06 +02:00
|
|
|
protected function getLocaleForUser(User $user): string
|
2022-09-02 20:19:01 +02:00
|
|
|
{
|
2023-09-17 14:29:06 +02:00
|
|
|
$default = config('app.default_locale');
|
2022-09-02 20:19:01 +02:00
|
|
|
|
2023-09-16 14:18:35 +02:00
|
|
|
if ($user->isGuest() && config('app.auto_detect_locale')) {
|
2023-09-17 14:29:06 +02:00
|
|
|
return $this->autoDetectLocale(request(), $default);
|
2022-09-02 20:19:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return setting()->getUser($user, 'language', $default);
|
|
|
|
}
|
|
|
|
|
2023-09-02 16:11:42 +02:00
|
|
|
/**
|
2023-09-17 14:29:06 +02:00
|
|
|
* Get a locale definition for the current user.
|
2023-09-02 16:11:42 +02:00
|
|
|
*/
|
2023-09-17 14:29:06 +02:00
|
|
|
public function getForUser(User $user): LocaleDefinition
|
2023-09-02 16:11:42 +02:00
|
|
|
{
|
2023-09-17 14:29:06 +02:00
|
|
|
$localeString = $this->getLocaleForUser($user);
|
2023-09-02 16:11:42 +02:00
|
|
|
|
2023-09-17 14:29:06 +02:00
|
|
|
return new LocaleDefinition(
|
|
|
|
$localeString,
|
2023-09-17 23:02:12 +02:00
|
|
|
$this->localeMap[$localeString] ?? $localeString,
|
2023-09-17 14:29:06 +02:00
|
|
|
in_array($localeString, $this->rtlLocales),
|
|
|
|
);
|
2022-09-02 20:19:01 +02: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 17:20:21 +02:00
|
|
|
$availableLocales = $this->getAllAppLocales();
|
2023-09-17 14:29:06 +02:00
|
|
|
|
2022-09-02 20:19:01 +02:00
|
|
|
foreach ($request->getLanguages() as $lang) {
|
|
|
|
if (in_array($lang, $availableLocales)) {
|
|
|
|
return $lang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
2023-09-17 17:20:21 +02:00
|
|
|
/**
|
|
|
|
* Get all the available app-specific level locale strings.
|
|
|
|
*/
|
|
|
|
public function getAllAppLocales(): array
|
|
|
|
{
|
|
|
|
return array_keys($this->localeMap);
|
|
|
|
}
|
2022-09-05 14:18:37 +02:00
|
|
|
}
|