Enabled translation when not logged in

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

Fixes #375
This commit is contained in:
Dan Brown 2017-04-29 16:47:41 +01:00
parent 92108d710d
commit ad4642c2c4
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9
6 changed files with 34 additions and 4 deletions

View file

@ -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);

View file

@ -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);

View file

@ -58,6 +58,7 @@ return [
*/
'locale' => env('APP_LANG', 'en'),
'locales' => ['en', 'de', 'es', 'fr', 'nl', 'pt_BR', 'sk'],
/*
|--------------------------------------------------------------------------

View file

@ -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.

View file

@ -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',

View file

@ -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()
{