| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | namespace BookStack\Translation; | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-02 22:11:42 +08:00
										 |  |  | use BookStack\Users\Models\User; | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-17 20:29:06 +08:00
										 |  |  | class LocaleManager | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-09-17 20:29:06 +08:00
										 |  |  |      * Array of right-to-left locale options. | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-09-17 20:29:06 +08:00
										 |  |  |     protected array $rtlLocales = ['ar', 'fa', 'he']; | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-09-18 05:02:12 +08:00
										 |  |  |      * Map of BookStack locale names to best-estimate ISO locale names. | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  |      * Locales can often be found by running `locale -a` on a linux system. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-09-18 05:02:12 +08:00
										 |  |  |      * @var array<string, string> | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected array $localeMap = [ | 
					
						
							| 
									
										
										
										
											2023-09-18 05:02:12 +08:00
										 |  |  |         'ar'          => 'ar', | 
					
						
							|  |  |  |         'bg'          => 'bg_BG', | 
					
						
							| 
									
										
										
										
											2024-12-23 19:27:58 +08:00
										 |  |  |         'bn'          => 'bn_BD', | 
					
						
							| 
									
										
										
										
											2023-09-18 05:02:12 +08:00
										 |  |  |         '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 19:41:36 +08:00
										 |  |  |         'fi'          => 'fi_FI', | 
					
						
							| 
									
										
										
										
											2023-09-18 05:02:12 +08:00
										 |  |  |         'fr'          => 'fr_FR', | 
					
						
							|  |  |  |         'he'          => 'he_IL', | 
					
						
							|  |  |  |         'hr'          => 'hr_HR', | 
					
						
							|  |  |  |         'hu'          => 'hu_HU', | 
					
						
							|  |  |  |         'id'          => 'id_ID', | 
					
						
							| 
									
										
										
										
											2024-12-23 19:27:58 +08:00
										 |  |  |         'is'          => 'is_IS', | 
					
						
							| 
									
										
										
										
											2023-09-18 05:02:12 +08:00
										 |  |  |         '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 19:41:36 +08:00
										 |  |  |         'nn'          => 'nn_NO', | 
					
						
							| 
									
										
										
										
											2023-09-18 05:02:12 +08: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 19:41:36 +08:00
										 |  |  |         'sq'          => 'sq_AL', | 
					
						
							| 
									
										
										
										
											2024-02-28 20:29:09 +08:00
										 |  |  |         'sr'          => 'sr_RS', | 
					
						
							| 
									
										
										
										
											2023-09-18 05:02:12 +08:00
										 |  |  |         'sv'          => 'sv_SE', | 
					
						
							| 
									
										
										
										
											2024-11-08 21:46:57 +08:00
										 |  |  |         'tk'          => 'tk_TM', | 
					
						
							| 
									
										
										
										
											2023-09-18 05:02:12 +08:00
										 |  |  |         'tr'          => 'tr_TR', | 
					
						
							|  |  |  |         'uk'          => 'uk_UA', | 
					
						
							|  |  |  |         'uz'          => 'uz_UZ', | 
					
						
							|  |  |  |         'vi'          => 'vi_VN', | 
					
						
							|  |  |  |         'zh_CN'       => 'zh_CN', | 
					
						
							|  |  |  |         'zh_TW'       => 'zh_TW', | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-09-17 20:29:06 +08:00
										 |  |  |      * Get the BookStack locale string for the given user. | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-09-17 20:29:06 +08:00
										 |  |  |     protected function getLocaleForUser(User $user): string | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-09-17 20:29:06 +08:00
										 |  |  |         $default = config('app.default_locale'); | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-16 20:18:35 +08:00
										 |  |  |         if ($user->isGuest() && config('app.auto_detect_locale')) { | 
					
						
							| 
									
										
										
										
											2023-09-17 20:29:06 +08:00
										 |  |  |             return $this->autoDetectLocale(request(), $default); | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return setting()->getUser($user, 'language', $default); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-02 22:11:42 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-09-17 20:29:06 +08:00
										 |  |  |      * Get a locale definition for the current user. | 
					
						
							| 
									
										
										
										
											2023-09-02 22:11:42 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-09-17 20:29:06 +08:00
										 |  |  |     public function getForUser(User $user): LocaleDefinition | 
					
						
							| 
									
										
										
										
											2023-09-02 22:11:42 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-09-17 20:29:06 +08:00
										 |  |  |         $localeString = $this->getLocaleForUser($user); | 
					
						
							| 
									
										
										
										
											2023-09-02 22:11:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-17 20:29:06 +08:00
										 |  |  |         return new LocaleDefinition( | 
					
						
							|  |  |  |             $localeString, | 
					
						
							| 
									
										
										
										
											2023-09-18 05:02:12 +08:00
										 |  |  |             $this->localeMap[$localeString] ?? $localeString, | 
					
						
							| 
									
										
										
										
											2023-09-17 20:29:06 +08:00
										 |  |  |             in_array($localeString, $this->rtlLocales), | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08: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 23:20:21 +08:00
										 |  |  |         $availableLocales = $this->getAllAppLocales(); | 
					
						
							| 
									
										
										
										
											2023-09-17 20:29:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-03 02:19:01 +08:00
										 |  |  |         foreach ($request->getLanguages() as $lang) { | 
					
						
							|  |  |  |             if (in_array($lang, $availableLocales)) { | 
					
						
							|  |  |  |                 return $lang; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $default; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-17 23:20:21 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get all the available app-specific level locale strings. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAllAppLocales(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return array_keys($this->localeMap); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-05 20:18:37 +08:00
										 |  |  | } |