Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA: Use two-letter language code for translation folders #2521

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
$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;

Check warning on line 48 in sourcecode/apis/contentauthor/app/Http/Middleware/LtiLocale.php

View check run for this annotation

Codecov / codecov/patch

sourcecode/apis/contentauthor/app/Http/Middleware/LtiLocale.php#L48

Added line #L48 was not covered by tests
}
}
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 @@
}
}

/**
* 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;

Check warning on line 288 in sourcecode/apis/contentauthor/app/Libraries/H5P/H5PConfigAbstract.php

View check run for this annotation

Codecov / codecov/patch

sourcecode/apis/contentauthor/app/Libraries/H5P/H5PConfigAbstract.php#L288

Added line #L288 was not covered by tests
} 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