Skip to content

Commit

Permalink
CA: Use two-letter language code for translation folders. Try and det…
Browse files Browse the repository at this point in the history
…ect if language files exist, both local and H5P Editor (#2521)
  • Loading branch information
chrieinv authored Sep 27, 2023
1 parent d4cb4ea commit ed5bb39
Show file tree
Hide file tree
Showing 95 changed files with 37 additions and 13 deletions.
23 changes: 20 additions & 3 deletions sourcecode/apis/contentauthor/app/Http/Middleware/LtiLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace App\Http\Middleware;

use Illuminate\Support\Facades\App;
use App\H5pLti;
use Closure;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Session;
use App\H5pLti;

class LtiLocale
{
Expand All @@ -23,11 +23,28 @@ public function handle($request, Closure $next)
$ltiRequest = $this->h5pLti->getValidatedLtiRequest();
if ($ltiRequest != null) {
if ($ltiRequest->getLocale()) {
// Store the original code, even if we don't have this locale, maybe H5P does
Session::put('locale', $ltiRequest->getLocale());
}
}
App::setLocale(Session::get('locale', config('app.fallback_locale')));
App::setLocale($this->resolveLocale(Session::get('locale', config('app.fallback_locale'))));

return $next($request);
}

/**
* Check if we have a translation for the code. If that failes, and a code longer than two characters is
* given, check if we have a translation for the two-code version. Failing that the original code is returned.
*/
private function resolveLocale(string $locale): string
{
if (!file_exists(resource_path('lang/' . $locale)) && strlen($locale) > 2) {
$lang = \Iso639p3::code2letters($locale);
if (file_exists(resource_path('lang/' . $lang))) {
return $lang;
}
}

return $locale;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Libraries\H5P\Interfaces\ConfigInterface;
use App\Libraries\H5P\Interfaces\H5PAdapterInterface;
use Illuminate\Support\Facades\Session;
use Iso639p3;
use Ramsey\Uuid\Uuid;

use function Cerpus\Helper\Helpers\profile as config;
Expand Down Expand Up @@ -272,22 +273,28 @@ protected function addCustomEditorStyles(): void
}
}

/**
* Language file to load for H5P Editor
*/
private function getLanguage(): string
{
$preferredH5PLanguage = LtiToH5PLanguage::convert(Session::get('locale'));

if ($this->languageFileExists($preferredH5PLanguage)) {
return "language/$preferredH5PLanguage.js";
}

return 'language/en.js';
return "language/" . $this->resolveEditorLocale(Session::get('locale')) . ".js";
}

private function languageFileExists($preferredLanguage): bool
private function resolveEditorLocale($locale): string
{
$path = base_path('vendor/h5p/h5p-editor/language/' . $preferredLanguage . '.js');
if (is_string($locale)) {
if (file_exists(base_path('vendor/h5p/h5p-editor/language/' . $locale . '.js'))) {
return $locale;
} elseif (strlen($locale) > 2) {
$lang = Iso639p3::code2letters($locale);
if (file_exists(base_path('vendor/h5p/h5p-editor/language/' . $lang . '.js'))) {
return $lang;
}
}
}

return file_exists($path);
return 'en';
}

private function getL10n(): array
Expand Down

0 comments on commit ed5bb39

Please sign in to comment.