Dan Brown

Enabled translation when not logged in

Reads from the Accept-Language HTTP header.
Also fixed some encoding for ES translations.

Fixes #375
...@@ -46,7 +46,7 @@ class HomeController extends Controller ...@@ -46,7 +46,7 @@ class HomeController extends Controller
46 * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response 46 * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
47 */ 47 */
48 public function getTranslations() { 48 public function getTranslations() {
49 - $locale = trans()->getLocale(); 49 + $locale = app()->getLocale();
50 $cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale; 50 $cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale;
51 if (cache()->has($cacheKey) && config('app.env') !== 'development') { 51 if (cache()->has($cacheKey) && config('app.env') !== 'development') {
52 $resp = cache($cacheKey); 52 $resp = cache($cacheKey);
......
...@@ -15,7 +15,17 @@ class Localization ...@@ -15,7 +15,17 @@ class Localization
15 public function handle($request, Closure $next) 15 public function handle($request, Closure $next)
16 { 16 {
17 $defaultLang = config('app.locale'); 17 $defaultLang = config('app.locale');
18 - $locale = setting()->getUser(user(), 'language', $defaultLang); 18 + if (user()->isDefault()) {
19 + $locale = $defaultLang;
20 + $availableLocales = config('app.locales');
21 + foreach ($request->getLanguages() as $lang) {
22 + if (!in_array($lang, $availableLocales)) continue;
23 + $locale = $lang;
24 + break;
25 + }
26 + } else {
27 + $locale = setting()->getUser(user(), 'language', $defaultLang);
28 + }
19 app()->setLocale($locale); 29 app()->setLocale($locale);
20 Carbon::setLocale($locale); 30 Carbon::setLocale($locale);
21 return $next($request); 31 return $next($request);
......
...@@ -58,6 +58,7 @@ return [ ...@@ -58,6 +58,7 @@ return [
58 */ 58 */
59 59
60 'locale' => env('APP_LANG', 'en'), 60 'locale' => env('APP_LANG', 'en'),
61 + 'locales' => ['en', 'de', 'es', 'fr', 'nl', 'pt_BR', 'sk'],
61 62
62 /* 63 /*
63 |-------------------------------------------------------------------------- 64 |--------------------------------------------------------------------------
......
...@@ -43,6 +43,8 @@ Once done you can run `phpunit` in the application root directory to run all tes ...@@ -43,6 +43,8 @@ Once done you can run `phpunit` in the application root directory to run all tes
43 ## Translations 43 ## Translations
44 44
45 As part of BookStack v0.14 support for translations has been built in. All text strings can be found in the `resources/lang` folder where each language option has its own folder. To add a new language you should copy the `en` folder to an new folder (eg. `fr` for french) then go through and translate all text strings in those files, leaving the keys and file-names intact. If a language string is missing then the `en` translation will be used. To show the language option in the user preferences language drop-down you will need to add your language to the options found at the bottom of the `resources/lang/en/settings.php` file. A system-wide language can also be set in the `.env` file like so: `APP_LANG=en`. 45 As part of BookStack v0.14 support for translations has been built in. All text strings can be found in the `resources/lang` folder where each language option has its own folder. To add a new language you should copy the `en` folder to an new folder (eg. `fr` for french) then go through and translate all text strings in those files, leaving the keys and file-names intact. If a language string is missing then the `en` translation will be used. To show the language option in the user preferences language drop-down you will need to add your language to the options found at the bottom of the `resources/lang/en/settings.php` file. A system-wide language can also be set in the `.env` file like so: `APP_LANG=en`.
46 +
47 +You will also need to add the language to the `locales` array in the `config/app.php` file.
46 48
47 Some strings have colon-prefixed variables in such as `:userName`. Leave these values as they are as they will be replaced at run-time. 49 Some strings have colon-prefixed variables in such as `:userName`. Leave these values as they are as they will be replaced at run-time.
48 50
......
...@@ -166,7 +166,7 @@ return [ ...@@ -166,7 +166,7 @@ return [
166 'start_a' => ':count usuarios han comenzado a editar esta página', 166 'start_a' => ':count usuarios han comenzado a editar esta página',
167 'start_b' => ':userName ha comenzado a editar esta página', 167 'start_b' => ':userName ha comenzado a editar esta página',
168 'time_a' => 'desde que las página fue actualizada', 168 'time_a' => 'desde que las página fue actualizada',
169 - 'time_b' => 'en los Ãltimos :minCount minutos', 169 + 'time_b' => 'en los últimos :minCount minutos',
170 'message' => ':start :time. Ten cuidado de no sobreescribir los cambios del otro usuario', 170 'message' => ':start :time. Ten cuidado de no sobreescribir los cambios del otro usuario',
171 ], 171 ],
172 'pages_draft_discarded' => 'Borrador descartado, el editor ha sido actualizado con el contenido de la página actual', 172 'pages_draft_discarded' => 'Borrador descartado, el editor ha sido actualizado con el contenido de la página actual',
...@@ -189,7 +189,7 @@ return [ ...@@ -189,7 +189,7 @@ return [
189 'attachments_set_link' => 'Setear Link', 189 'attachments_set_link' => 'Setear Link',
190 'attachments_delete_confirm' => 'Haga click en borrar nuevamente para confirmar que quiere borrar este adjunto.', 190 'attachments_delete_confirm' => 'Haga click en borrar nuevamente para confirmar que quiere borrar este adjunto.',
191 'attachments_dropzone' => 'Arrastre ficheros aquío haga click aquípara adjuntar un fichero', 191 'attachments_dropzone' => 'Arrastre ficheros aquío haga click aquípara adjuntar un fichero',
192 - 'attachments_no_files' => 'NingÃn fichero ha sido adjuntado', 192 + 'attachments_no_files' => 'Ningún fichero ha sido adjuntado',
193 'attachments_explain_link' => 'Ud. puede agregar un link o si lo prefiere puede agregar un fichero. Esto puede ser un link a otra página o un link a un fichero en la nube.', 193 'attachments_explain_link' => 'Ud. puede agregar un link o si lo prefiere puede agregar un fichero. Esto puede ser un link a otra página o un link a un fichero en la nube.',
194 'attachments_link_name' => 'Nombre de Link', 194 'attachments_link_name' => 'Nombre de Link',
195 'attachment_link' => 'Link adjunto', 195 'attachment_link' => 'Link adjunto',
......
...@@ -14,6 +14,23 @@ class LanguageTest extends TestCase ...@@ -14,6 +14,23 @@ class LanguageTest extends TestCase
14 $this->langs = array_diff(scandir(resource_path('lang')), ['..', '.']); 14 $this->langs = array_diff(scandir(resource_path('lang')), ['..', '.']);
15 } 15 }
16 16
17 + public function test_locales_config_key_set_properly()
18 + {
19 + $configLocales = config('app.locales');
20 + sort($configLocales);
21 + sort($this->langs);
22 + $this->assertTrue(implode(':', $this->langs) === implode(':', $configLocales), 'app.locales configuration variable matches found lang files');
23 + }
24 +
25 + public function test_correct_language_if_not_logged_in()
26 + {
27 + $loginReq = $this->get('/login');
28 + $loginReq->assertSee('Log In');
29 +
30 + $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
31 + $loginPageFrenchReq->assertSee('Se Connecter');
32 + }
33 +
17 public function test_js_endpoint_for_each_language() 34 public function test_js_endpoint_for_each_language()
18 { 35 {
19 36
......