namespace App\Elearning;
use App\Elearning\QuizCompiler\Animations;
+use App\Elearning\QuizCompiler\L10N;
use App\Fields\FluidbookFont;
use App\Jobs\Base;
use App\Models\Quiz;
{
use Animations;
+ use L10N;
/**
* @var Quiz
// Add data related to the current quiz in the "to compile" files
$this->writeStyles();
+ $this->writeL10n();
$this->writeData();
// Run the compiler
$this->runWebpack();
$vdir->copyDirectory($this->compilePath . '/dist/js', 'js');
$vdir->copyDirectory($this->compilePath . '/dist/assets', 'assets');
-
- $blade = new Blade($this->_resourcesPath() . '/views', $this->_resourcesPath() . '/cache/'.md5(rand(100000,10000000)).'/');
- $html = $blade->make('index', ['data' => $this->data, 'quiz' => $this->quiz, 'theme' => $this->theme])->render();
+ $l10n = $this->data->l10n;
+ $blade = new Blade($this->_resourcesPath() . '/views', $this->_resourcesPath() . '/cache/' . md5(rand(100000, 10000000)) . '/');
+ $html = $blade->make('index', ['data' => $this->data, 'quiz' => $this->quiz, 'theme' => $this->theme, '__' => function ($str) use ($l10n) {
+ return $l10n[$str] ?? $str;
+ }])->render();
$vdir->file_put_contents('index.html', $html);
--- /dev/null
+<?php
+
+namespace App\Elearning\QuizCompiler;
+
+use App\Models\ElearningTranslate;
+
+trait L10N
+{
+ protected function writeL10n()
+ {
+ $this->data->setRaw('l10n', $this->getTranslations());
+ }
+
+ protected function getTranslations()
+ {
+ $res= ElearningTranslate::getLocaleTranslations($this->data->translation, true);
+ return $res;
+ }
+}
--- /dev/null
+<?php
+
+namespace App\Fields;
+
+use Cubist\Backpack\CubistBackpackServiceProvider;
+use Cubist\Backpack\Magic\Fields\Locale;
+use Cubist\Util\Files\Files;
+
+class ContentLocale extends Locale
+{
+ protected $_columnType = 'fromfield';
+ protected $_columnViewNamespace = CubistBackpackServiceProvider::NAMESPACE . '::columns';
+
+ protected $_name = 'content';
+
+ public function __construct($attributes)
+ {
+ $list = Files::getDirectoryIterator(resource_path('lang'));
+ $this->_limitToLocales = [];
+ foreach ($list as $item) {
+ if (preg_match('/' . $this->_name . '.([a-z_A-Z]{2,5}).json/', $item->getFilename(), $matches)) {
+ $this->_limitToLocales[] = $matches[1];
+ }
+ }
+ $this->_limitToLocales = array_unique(array_merge(
+ array_values($this->_limitToLocales),
+ array_values($this->getAdditionalLocales()))
+ );
+ parent::__construct($attributes);
+ }
+
+
+ public function filterColumn($value)
+ {
+ return mb_strtoupper($value);
+ }
+
+ protected function getAdditionalLocales()
+ {
+ return [];
+ }
+}
--- /dev/null
+<?php
+
+
+namespace App\Fields;
+
+class ElearningLocale extends ContentLocale
+{
+
+ protected $_name = 'elearning';
+
+ protected function getAdditionalLocales()
+ {
+ return ['en','fr','de','es','hr','it','nl','pl','pt','sv','tr','cs','ja','no','ru','zh','ko','hi','ro'];
+ }
+
+}
namespace App\Fields;
-
-use Cubist\Backpack\CubistBackpackServiceProvider;
-use Cubist\Backpack\Magic\Fields\Locale;
-use Cubist\Util\Files\Files;
-
-class FluidbookLocale extends Locale
+class FluidbookLocale extends ContentLocale
{
- protected $_columnType = 'fromfield';
- protected $_columnViewNamespace = CubistBackpackServiceProvider::NAMESPACE . '::columns';
- public function __construct($attributes)
- {
- $list = Files::getDirectoryIterator(resource_path('lang'));
- $this->_limitToLocales = [];
- foreach ($list as $item) {
- if (preg_match('/fluidbook.([a-z_A-Z]{2,5}).json/', $item->getFilename(), $matches)) {
- $this->_limitToLocales[] = $matches[1];
- }
- }
- parent::__construct($attributes);
- }
+ protected $_name='fluidbook';
- public function filterColumn($value)
- {
- return mb_strtoupper($value);
- }
}
return static::$_name;
}
-
protected function _getLanguageFile($locale)
{
return static::getLanguageFile($locale);
return parent::getExtensions() + ['js'];
}
+ protected static function _getCacheKey()
+ {
+ return 'all_' . static::$_name . '_translations';
+ }
+
public static function getAllTranslations($force = true)
{
if (null === static::$_allTranslations) {
- $cacheKey = 'all_' . static::$_name . '_translations';
+ $cacheKey = static::_getCacheKey();
if ($force) {
Cache::forget($cacheKey);
}
$t->save();
}
+ public function onSaved(): bool
+ {
+ Cache::forget(static::_getCacheKey());
+ return parent::onSaved();
+ }
+
/**
* @param string $locale
* @return array[]|null
*/
- public static function getLocaleTranslations($locale)
+ public static function getLocaleTranslations($locale, $compiled = false)
{
$all = static::getAllTranslations(false);
- return $all[$locale] ?? [];
+ $res = $all[$locale] ?? [];
+ if (!$compiled) {
+ return $res;
+ }
+ $comp = [];
+ foreach ($res as $k => $v) {
+ if (is_string($v)) {
+ $comp[$k] = $v;
+ } else {
+ $comp[$v['str']] = $v['translation'];
+ }
+ }
+ return $comp;
}
public function getPaths()
protected $table = 'elearning_translate';
protected static string $_name = 'elearning';
- protected static $_basePath='resources/quiz/player';
+ protected static $_basePath = 'resources/quiz/player';
protected $_enableRevisions = false;
use FluidbookPlayerBranches;
protected $table = 'fluidbook_translate';
- protected static string $_name = 'elearning';
+ protected static string $_name = 'fluidbook';
protected static $_basePath = 'resources/fluidbookpublication/player';
protected static $_allTranslations = null;
namespace App\Models;
use App\Elearning\QuizCompiler;
+use App\Fields\ElearningLocale;
use App\Fields\QuizDevelopmentVersion;
use App\Fields\SCORMVersion;
use App\Http\Controllers\Admin\Operations\ChangeownerOperation;
];
}
- protected static function _getMessages()
- {
- return [
- 'defaultMessage' => __('Message affiché à la fin du quiz'),
- 'passedMessage' => __('Message affiché lors de la réussite du quiz'),
- 'failedMessage' => __('Message affiché lors de l\'échec du quiz'),
- ];
- }
protected static function _getImages()
{
'database_default' => 'quiz',
]);
- $this->addField('section_translation', FormSection::class, __('Langue et traductions'));
+ $this->addField('section_translation', FormSection::class, __('Langue'));
$this->addField(['name' => 'translation',
'label' => __('Langue'),
- 'type' => 'SelectFromModel',
- 'optionsmodel' => QuizTranslation::class,
- 'attribute' => 'locale',
+ 'type' => ElearningLocale::class,
'column' => true,
'column_label' => '<i class="fa fa-language"></i>']);
- foreach (self::_getMessages() as $name => $label) {
- $this->addField(['name' => $name,
- 'label' => $label,
- 'hint' => __('Laisser vide pour utiliser le message par défaut'),
- 'type' => 'Text',
- ]);
- }
+// foreach (self::_getMessages() as $name => $label) {
+// $this->addField(['name' => $name,
+// 'label' => $label,
+// 'hint' => __('Laisser vide pour utiliser le message par défaut'),
+// 'type' => 'Text',
+// ]);
+// }
$this->addField('section_scorm', FormSection::class, __('SCORM'));
}
// Create data.js
- $d['translations'] = [];
- $tid = $data->get('translation', 1) ?? 1;
- if ($tid === 'en') {
- $tid = 1;
- }
- $translation = QuizTranslation::find($tid);
- foreach (QuizTranslation::getTexts() as $text => $default) {
- $d['translations'][$text] = $translation->getAttribute($text);
- }
- foreach (self::getMessages() as $name => $message) {
- if (null === $d[$name] || !$d[$name]) {
- $d[$name] = $translation->getAttribute($name);
- }
- }
-
+ $locale = $data->get('translation', 'en') ?? 'en';
// Countries
- $d['countriesList'] = \Cubist\Locale\Country::getList($translation->getAttribute('locale'));
+ $d['countriesList'] = \Cubist\Locale\Country::getList($locale);
// Fix boolean
$booleans = ['multiple' => false, 'required' => true, 'count_for_score' => true];
return $d;
}
- public static function getMessages()
- {
- return self::_getMessages();
- }
-
public static function getColors()
{
return self::_getColors();
{
return QuizTheme::withoutGlobalScopes()->find($this->theme ?: 3);
}
+
+ public function onRetrieved(): bool
+ {
+ $map = ['', 'en', 'fr', 'de', 'es', 'hr', 'it', 'nl', 'pl', 'pt',
+ 'sv', 'tr', 'cs', 'ja', 'no', 'ru', 'zh', 'ko', 'hi', '', 'ro'];
+
+ $res = parent::onRetrieved();
+ if (is_numeric($this->translation) && isset($map[$this->translation])) {
+ $this->translation = $map[$this->translation];
+ $this->setAttribute('translation', $this->translation);
+ }
+ return $res;
+ }
}