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;
|
2023-09-17 08:29:06 -04:00
|
|
|
use Carbon\Carbon;
|
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 08:29:06 -04:00
|
|
|
* Map of BookStack locale names to best-estimate ISO and windows locale names.
|
2022-09-02 14:19:01 -04:00
|
|
|
* Locales can often be found by running `locale -a` on a linux system.
|
|
|
|
* Windows locales can be found at:
|
2022-09-05 08:18:37 -04:00
|
|
|
* https://docs.microsoft.com/en-us/cpp/c-runtime-library/language-strings?view=msvc-170.
|
2022-09-02 14:19:01 -04:00
|
|
|
*
|
|
|
|
* @var array<string, array{iso: string, windows: string}>
|
|
|
|
*/
|
|
|
|
protected array $localeMap = [
|
|
|
|
'ar' => ['iso' => 'ar', 'windows' => 'Arabic'],
|
|
|
|
'bg' => ['iso' => 'bg_BG', 'windows' => 'Bulgarian'],
|
|
|
|
'bs' => ['iso' => 'bs_BA', 'windows' => 'Bosnian (Latin)'],
|
|
|
|
'ca' => ['iso' => 'ca', 'windows' => 'Catalan'],
|
2023-02-27 14:09:20 -05:00
|
|
|
'cs' => ['iso' => 'cs_CZ', 'windows' => 'Czech'],
|
2022-09-02 14:19:01 -04:00
|
|
|
'da' => ['iso' => 'da_DK', 'windows' => 'Danish'],
|
|
|
|
'de' => ['iso' => 'de_DE', 'windows' => 'German'],
|
|
|
|
'de_informal' => ['iso' => 'de_DE', 'windows' => 'German'],
|
2022-10-20 07:25:02 -04:00
|
|
|
'el' => ['iso' => 'el_GR', 'windows' => 'Greek'],
|
2023-09-17 08:29:06 -04:00
|
|
|
'en' => ['iso' => 'en_GB', 'windows' => 'English'],
|
2022-09-02 14:19:01 -04:00
|
|
|
'es' => ['iso' => 'es_ES', 'windows' => 'Spanish'],
|
|
|
|
'es_AR' => ['iso' => 'es_AR', 'windows' => 'Spanish'],
|
|
|
|
'et' => ['iso' => 'et_EE', 'windows' => 'Estonian'],
|
|
|
|
'eu' => ['iso' => 'eu_ES', 'windows' => 'Basque'],
|
|
|
|
'fa' => ['iso' => 'fa_IR', 'windows' => 'Persian'],
|
|
|
|
'fr' => ['iso' => 'fr_FR', 'windows' => 'French'],
|
|
|
|
'he' => ['iso' => 'he_IL', 'windows' => 'Hebrew'],
|
|
|
|
'hr' => ['iso' => 'hr_HR', 'windows' => 'Croatian'],
|
|
|
|
'hu' => ['iso' => 'hu_HU', 'windows' => 'Hungarian'],
|
|
|
|
'id' => ['iso' => 'id_ID', 'windows' => 'Indonesian'],
|
|
|
|
'it' => ['iso' => 'it_IT', 'windows' => 'Italian'],
|
|
|
|
'ja' => ['iso' => 'ja', 'windows' => 'Japanese'],
|
|
|
|
'ko' => ['iso' => 'ko_KR', 'windows' => 'Korean'],
|
|
|
|
'lt' => ['iso' => 'lt_LT', 'windows' => 'Lithuanian'],
|
|
|
|
'lv' => ['iso' => 'lv_LV', 'windows' => 'Latvian'],
|
|
|
|
'nb' => ['iso' => 'nb_NO', 'windows' => 'Norwegian (Bokmal)'],
|
2023-09-17 08:29:06 -04:00
|
|
|
'nl' => ['iso' => 'nl_NL', 'windows' => 'Dutch'],
|
2022-09-02 14:19:01 -04:00
|
|
|
'pl' => ['iso' => 'pl_PL', 'windows' => 'Polish'],
|
|
|
|
'pt' => ['iso' => 'pt_PT', 'windows' => 'Portuguese'],
|
|
|
|
'pt_BR' => ['iso' => 'pt_BR', 'windows' => 'Portuguese'],
|
2022-09-06 12:41:32 -04:00
|
|
|
'ro' => ['iso' => 'ro_RO', 'windows' => 'Romanian'],
|
2022-09-02 14:19:01 -04:00
|
|
|
'ru' => ['iso' => 'ru', 'windows' => 'Russian'],
|
|
|
|
'sk' => ['iso' => 'sk_SK', 'windows' => 'Slovak'],
|
|
|
|
'sl' => ['iso' => 'sl_SI', 'windows' => 'Slovenian'],
|
|
|
|
'sv' => ['iso' => 'sv_SE', 'windows' => 'Swedish'],
|
2023-09-17 08:29:06 -04:00
|
|
|
'tr' => ['iso' => 'tr_TR', 'windows' => 'Turkish'],
|
2022-09-02 14:19:01 -04:00
|
|
|
'uk' => ['iso' => 'uk_UA', 'windows' => 'Ukrainian'],
|
2023-09-13 05:52:12 -04:00
|
|
|
'uz' => ['iso' => 'uz_UZ', 'windows' => 'Uzbek'],
|
2022-09-02 14:19:01 -04:00
|
|
|
'vi' => ['iso' => 'vi_VN', 'windows' => 'Vietnamese'],
|
|
|
|
'zh_CN' => ['iso' => 'zh_CN', 'windows' => 'Chinese (Simplified)'],
|
|
|
|
'zh_TW' => ['iso' => 'zh_TW', 'windows' => 'Chinese (Traditional)'],
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
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,
|
|
|
|
$this->getIsoName($localeString),
|
|
|
|
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 08:29:06 -04:00
|
|
|
$availableLocales = array_keys($this->localeMap);
|
|
|
|
|
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 08:29:06 -04:00
|
|
|
* Get the ISO version of a BookStack locale.
|
|
|
|
*/
|
|
|
|
protected function getIsoName(string $locale): string
|
|
|
|
{
|
|
|
|
return $this->localeMap[$locale]['iso'] ?? $locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the active locale for system level components.
|
2022-09-02 14:19:01 -04:00
|
|
|
*/
|
2023-09-17 08:29:06 -04:00
|
|
|
public function setAppLocale(LocaleDefinition $locale): void
|
2022-09-02 14:19:01 -04:00
|
|
|
{
|
2023-09-17 08:29:06 -04:00
|
|
|
app()->setLocale($locale->appLocale());
|
|
|
|
Carbon::setLocale($locale->isoLocale());
|
|
|
|
$this->setPhpDateTimeLocale($locale);
|
2022-09-02 14:19:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the system date locale for localized date formatting.
|
|
|
|
* Will try both the standard locale name and the UTF8 variant.
|
|
|
|
*/
|
2023-09-17 08:29:06 -04:00
|
|
|
public function setPhpDateTimeLocale(LocaleDefinition $locale): void
|
2022-09-02 14:19:01 -04:00
|
|
|
{
|
2023-09-17 08:29:06 -04:00
|
|
|
$appLocale = $locale->appLocale();
|
|
|
|
$isoLocale = $this->localeMap[$appLocale]['iso'] ?? '';
|
|
|
|
$isoLocalePrefix = explode('_', $isoLocale)[0];
|
2022-09-02 14:19:01 -04:00
|
|
|
|
2023-02-27 14:09:20 -05:00
|
|
|
$locales = array_values(array_filter([
|
2023-09-17 08:29:06 -04:00
|
|
|
$isoLocale ? $isoLocale . '.utf8' : false,
|
|
|
|
$isoLocale ?: false,
|
|
|
|
$isoLocale ? str_replace('_', '-', $isoLocale) : false,
|
|
|
|
$isoLocale ? $isoLocalePrefix . '.UTF-8' : false,
|
|
|
|
$this->localeMap[$appLocale]['windows'] ?? false,
|
|
|
|
$appLocale,
|
2023-02-27 14:09:20 -05:00
|
|
|
]));
|
2022-09-02 14:19:01 -04:00
|
|
|
|
2022-09-05 08:33:05 -04:00
|
|
|
if (!empty($locales)) {
|
2023-02-06 15:00:44 -05:00
|
|
|
setlocale(LC_TIME, $locales[0], ...array_slice($locales, 1));
|
2022-09-05 08:33:05 -04:00
|
|
|
}
|
2022-09-02 14:19:01 -04:00
|
|
|
}
|
2022-09-05 08:18:37 -04:00
|
|
|
}
|