Skip to content

Commit

Permalink
Merge branch 'master' into hubbub
Browse files Browse the repository at this point in the history
  • Loading branch information
emmachughes committed Sep 27, 2023
2 parents b7efdf1 + ed5bb39 commit 637dfb4
Show file tree
Hide file tree
Showing 96 changed files with 38 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 = public_path('h5p-editor-php-library/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
1 change: 1 addition & 0 deletions sourcecode/apis/contentauthor/config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
'local' => [ // local app storage
'driver' => 'local',
'root' => storage_path('app'),
'url' => '/h5pstorage/',
],
'testDisk' => [ // disk where tests are located.
'driver' => 'local',
Expand Down

0 comments on commit 637dfb4

Please sign in to comment.