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

editor field label overrides using alter_semantics hook do only work for English #126

Open
otacke opened this issue Jan 14, 2021 · 0 comments

Comments

@otacke
Copy link
Contributor

otacke commented Jan 14, 2021

When using the alter_semantics hook, overriding field labels does only work when the platform is set to English.

cause

When loading the editor, the getLibraryData function

public function getLibraryData($machineName, $majorVersion, $minorVersion, $languageCode, $prefix = '', $fileDir = '', $defaultLanguage) {
$libraryData = new stdClass();
$library = $this->h5p->loadLibrary($machineName, $majorVersion, $minorVersion);
// Include name and version in data object for convenience
$libraryData->name = $library['machineName'];
$libraryData->version = (object) array('major' => $library['majorVersion'], 'minor' => $library['minorVersion']);
$libraryData->title = $library['title'];
$libraryData->upgradesScript = $this->h5p->fs->getUpgradeScript($library['machineName'], $library['majorVersion'], $library['minorVersion']);
if ($libraryData->upgradesScript !== NULL) {
// If valid add URL prefix
$libraryData->upgradesScript = $this->h5p->url . $prefix . $libraryData->upgradesScript;
}
$libraries = $this->findEditorLibraries($library['machineName'], $library['majorVersion'], $library['minorVersion']);
$libraryData->semantics = $this->h5p->loadLibrarySemantics($library['machineName'], $library['majorVersion'], $library['minorVersion']);
$libraryData->language = $this->getLibraryLanguage($library['machineName'], $library['majorVersion'], $library['minorVersion'], $languageCode);
$libraryData->defaultLanguage = empty($defaultLanguage) ? NULL : $this->getLibraryLanguage($library['machineName'], $library['majorVersion'], $library['minorVersion'], $defaultLanguage);
$libraryData->languages = $this->storage->getAvailableLanguages($library['machineName'], $library['majorVersion'], $library['minorVersion']);
// Temporarily disable asset aggregation
$aggregateAssets = $this->h5p->aggregateAssets;
$this->h5p->aggregateAssets = FALSE;
// This is done to prevent files being loaded multiple times due to how
// the editor works.
// Get list of JS and CSS files that belongs to the dependencies
$files = $this->h5p->getDependenciesFiles($libraries, $prefix);
$libraryName = H5PCore::libraryToString(compact('machineName', 'majorVersion', 'minorVersion'), true);
if ($this->hasPresave($libraryName) === true) {
$this->addPresaveFile($files, $library, $prefix);
}
$this->storage->alterLibraryFiles($files, $libraries);
// Restore asset aggregation setting
$this->h5p->aggregateAssets = $aggregateAssets;
// Create base URL
$url = $this->h5p->url;
// Javascripts
if (!empty($files['scripts'])) {
foreach ($files['scripts'] as $script) {
if (preg_match('/:\/\//', $script->path) === 1) {
// External file
$libraryData->javascript[] = $script->path . $script->version;
}
else {
// Local file
$path = $url . $script->path;
if (!isset($this->h5p->h5pD)) {
$path .= $script->version;
}
$libraryData->javascript[] = $path;
}
}
}
// Stylesheets
if (!empty($files['styles'])) {
foreach ($files['styles'] as $css) {
if (preg_match('/:\/\//', $css->path) === 1) {
// External file
$libraryData->css[] = $css->path . $css->version;
}
else {
// Local file
$path = $url . $css->path;
if (!isset($this->h5p->h5pD)) {
$path .= $css->version;
}
$libraryData->css[] = $path;
}
}
}
$translations = array();
// Add translations for libraries.
foreach ($libraries as $library) {
if (empty($library['semantics'])) {
$translation = $this->getLibraryLanguage($library['machineName'], $library['majorVersion'], $library['minorVersion'], $languageCode);
// If translation was not found, and this is not the English one, try to load
// the English translation
if ($translation === NULL && $languageCode !== 'en') {
$translation = $this->getLibraryLanguage($library['machineName'], $library['majorVersion'], $library['minorVersion'], 'en');
}
if ($translation !== NULL) {
$translations[$library['machineName']] = json_decode($translation);
}
}
}
$libraryData->translations = $translations;
return $libraryData;
}

is used to gather information to be passed to the browser, among them

  1. the semantics structure
    $libraryData->semantics = $this->h5p->loadLibrarySemantics($library['machineName'], $library['majorVersion'], $library['minorVersion']);

    and
  2. the translated labels for the platform's default language
    $libraryData->language = $this->getLibraryLanguage($library['machineName'], $library['majorVersion'], $library['minorVersion'], $languageCode);

If labels are supposed to be overridden using the alter_semantics hook, then $libraryData->semantics already contains the overrides. Both the semantics structure ($libraryData->semantics) and the translated labels ($libraryData->language) will end up in the browser, where in

var semantics = ns.$.extend(true, [], libraryData.semantics, languageSemantics);

the labels of the semantics will be overridden by the translation labels, thus overwriting the changes that were supposed to be set for the labels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant