Skip to content

NOW MANAGED ON CODEBERG

Enabled translation when not logged in

Dan Brownad4642c2c425
diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php
index 7892fe8ae..7f60d7009 100644
--- a/app/Http/Controllers/HomeController.php
+++ b/app/Http/Controllers/HomeController.php
@@ -46,7 +46,7 @@ class HomeController extends Controller
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
*/
public function getTranslations() {
- $locale = trans()->getLocale();
+ $locale = app()->getLocale();
$cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale;
if (cache()->has($cacheKey) && config('app.env') !== 'development') {
$resp = cache($cacheKey);
diff --git a/app/Http/Middleware/Localization.php b/app/Http/Middleware/Localization.php
index 31cb5d9a2..14c87c377 100644
--- a/app/Http/Middleware/Localization.php
+++ b/app/Http/Middleware/Localization.php
@@ -15,7 +15,17 @@ class Localization
public function handle($request, Closure $next)
{
$defaultLang = config('app.locale');
- $locale = setting()->getUser(user(), 'language', $defaultLang);
+ if (user()->isDefault()) {
+ $locale = $defaultLang;
+ $availableLocales = config('app.locales');
+ foreach ($request->getLanguages() as $lang) {
+ if (!in_array($lang, $availableLocales)) continue;
+ $locale = $lang;
+ break;
+ }
+ } else {
+ $locale = setting()->getUser(user(), 'language', $defaultLang);
+ }
app()->setLocale($locale);
Carbon::setLocale($locale);
return $next($request);
diff --git a/config/app.php b/config/app.php
index e70724dce..54cdca21b 100644
--- a/config/app.php
+++ b/config/app.php
@@ -58,6 +58,7 @@ return [
*/
'locale' => env('APP_LANG', 'en'),
+ 'locales' => ['en', 'de', 'es', 'fr', 'nl', 'pt_BR', 'sk'],
/*
|--------------------------------------------------------------------------
diff --git a/readme.md b/readme.md
index 3e269e175..e2f16e171 100644
--- a/readme.md
+++ b/readme.md
@@ -43,6 +43,8 @@ Once done you can run `phpunit` in the application root directory to run all tes
## Translations
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`.
+
+You will also need to add the language to the `locales` array in the `config/app.php` file.
Some strings have colon-prefixed variables in such as `:userName`. Leave these values as they are as they will be replaced at run-time.
diff --git a/resources/lang/es/entities.php b/resources/lang/es/entities.php
index 5d6a25fae..d6b2810bc 100644
--- a/resources/lang/es/entities.php
+++ b/resources/lang/es/entities.php
@@ -166,7 +166,7 @@ return [
'start_a' => ':count usuarios han comenzado a editar esta página',
'start_b' => ':userName ha comenzado a editar esta página',
'time_a' => 'desde que las página fue actualizada',
- 'time_b' => 'en los �ltimos :minCount minutos',
+ 'time_b' => 'en los últimos :minCount minutos',
'message' => ':start :time. Ten cuidado de no sobreescribir los cambios del otro usuario',
],
'pages_draft_discarded' => 'Borrador descartado, el editor ha sido actualizado con el contenido de la página actual',
@@ -189,7 +189,7 @@ return [
'attachments_set_link' => 'Setear Link',
'attachments_delete_confirm' => 'Haga click en borrar nuevamente para confirmar que quiere borrar este adjunto.',
'attachments_dropzone' => 'Arrastre ficheros aquío haga click aquípara adjuntar un fichero',
- 'attachments_no_files' => 'Ning�n fichero ha sido adjuntado',
+ 'attachments_no_files' => 'Ningún fichero ha sido adjuntado',
'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.',
'attachments_link_name' => 'Nombre de Link',
'attachment_link' => 'Link adjunto',
diff --git a/tests/LanguageTest.php b/tests/LanguageTest.php
index 911ac3e81..bb98a17b0 100644
--- a/tests/LanguageTest.php
+++ b/tests/LanguageTest.php
@@ -14,6 +14,23 @@ class LanguageTest extends TestCase
$this->langs = array_diff(scandir(resource_path('lang')), ['..', '.']);
}
+ public function test_locales_config_key_set_properly()
+ {
+ $configLocales = config('app.locales');
+ sort($configLocales);
+ sort($this->langs);
+ $this->assertTrue(implode(':', $this->langs) === implode(':', $configLocales), 'app.locales configuration variable matches found lang files');
+ }
+
+ public function test_correct_language_if_not_logged_in()
+ {
+ $loginReq = $this->get('/login');
+ $loginReq->assertSee('Log In');
+
+ $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
+ $loginPageFrenchReq->assertSee('Se Connecter');
+ }
+
public function test_js_endpoint_for_each_language()
{