Skip to content

Commit

Permalink
new LocaleTraits + fix fallback display names
Browse files Browse the repository at this point in the history
  • Loading branch information
ferishili committed Nov 20, 2023
1 parent 8d4f167 commit 058c399
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected function initForm()
// F_DISPLAY_NAME
$max_lenght = 20;
$display_name = (!empty($this->object->getDisplayName()) ? $this->object->getDisplayName() : '{added display name}');
$info = sprintf($this->parent_gui->txt(self::F_DISPLAY_NAME . '_info'), $max_lenght, strtolower($display_name));
$info = sprintf($this->plugin->txt('publication_usage_sub_' . self::F_DISPLAY_NAME . '_info'), $max_lenght, strtolower($display_name));
$te = new ilTextInputGUI($this->parent_gui->txt(self::F_DISPLAY_NAME), self::F_DISPLAY_NAME);
$te->setInfo($info);
$te->setMaxLength($max_lenght);
Expand Down
7 changes: 5 additions & 2 deletions classes/Event/class.xoctEventRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use srag\Plugins\Opencast\UI\Modal\EventModals;
use srag\Plugins\Opencast\Model\DTO\DownloadDto;
use srag\Plugins\Opencast\LegacyHelpers\TranslatorTrait;
use srag\Plugins\Opencast\Util\Locale\LocaleTrait;

/**
* Class xoctEventRenderer
Expand All @@ -26,6 +27,7 @@
class xoctEventRenderer
{
use TranslatorTrait;
use LocaleTrait;

public const LANG_MODULE = 'event';
/**
Expand Down Expand Up @@ -136,9 +138,10 @@ public function renderDropdowns(&$tpl): void
$content['link']
);
}
$display_name = PublicationUsageGroupRepository::getLocalizedDisplayName($group_data['display_name']);
$display_name = $this->getLocaleString(strtolower($group_data['display_name']),
PublicationUsageGroup::DISPLAY_NAME_LANG_MODULE, $group_data['display_name']);
if (empty($display_name)) {
$display_name = $this->translate('default', PublicationUsageGroup::DISPLAY_NAME_LANG_MODULE);
$display_name = $this->getLocaleString('default', PublicationUsageGroup::DISPLAY_NAME_LANG_MODULE);
}
$dropdown = $this->factory->dropdown()->standard(
$items
Expand Down
3 changes: 2 additions & 1 deletion lang/ilias_de.lang
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ channel_type#:#Serie
file#:#Datei
link#:#URL
subtab_publication_usage#:#Publikationsnutzungen
subtab_publication_sub_usage#:#Unternutzungen
subtab_publication_sub_usage#:#Unternutzungen
subtab_publication_groups#:#Unternutzungsgruppen
publication_usage_msg_success_sub#:#Unternutzung gespeichert
publication_usage_select_sub_usage_id#:#Unternutzungen
Expand All @@ -593,6 +593,7 @@ publication_usage_no_identifier#:#Keine Unternutzung gefunden!
publication_usage_sub#:#Unternutzung
publication_usage_display_name#:#Anzeigename
publication_usage_display_name_info#:#Anzeigename, der in dem Button angezeigt wird. (Maximal %s Zeichen, kann übersetzt werden, wenn (pu_display_name_%s) in eine(r) Sprachdatei gespeichert wird.)
publication_usage_sub_display_name_info#:#Anzeigename, der in dem Button angezeigt wird. (Maximal %s Zeichen, kann übersetzt werden, wenn (pus_display_name_%s) in eine(r) Sprachdatei gespeichert wird.)
publication_usage_group_id#:#Unternutzungsgruppe
publication_usage_add_new_sub#:#untergeordnete Nutzung hinzufügen
publication_usage_add_new_group#:#Neue Unternutzungsgruppe hinzufügen
Expand Down
1 change: 1 addition & 0 deletions lang/ilias_en.lang
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ publication_usage_no_identifier#:#No publication usage identifier found!
publication_usage_sub#:#Sub
publication_usage_display_name#:#Display Name
publication_usage_display_name_info#:#The display name (max %s chars) to be shown to the users, and it can be localized by adding a string id (pu_display_name_%s) to the plugin languages.
publication_usage_sub_display_name_info#:#The display name (max %s chars) to be shown to the users, and it can be localized by adding a string id (pus_display_name_%s) to the plugin languages.
publication_usage_group_id#:#Publication Group
publication_usage_add_new_group#:#Add new group
publication_usage_group_name#:#Name
Expand Down
12 changes: 5 additions & 7 deletions src/Model/Publication/Config/PublicationSubUsageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace srag\Plugins\Opencast\Model\Publication\Config;

use srag\Plugins\Opencast\LegacyHelpers\TranslatorTrait;
use srag\Plugins\Opencast\Util\Locale\LocaleTrait;

/**
* Class PublicationSubUsageRepository
Expand All @@ -11,7 +11,7 @@
*/
class PublicationSubUsageRepository
{
use TranslatorTrait;
use LocaleTrait;
/**
* Returns the display name of the sub-usage but by looking for a record also in localization.
* @param int $sub_id
Expand All @@ -23,10 +23,8 @@ public function getDisplayName(int $sub_id): string
$sub_usage = PublicationSubUsage::find($sub_id);
if (!empty($sub_usage) && !empty($sub_usage->getDisplayName())) {
$display_name = $sub_usage->getDisplayName();
$translated_display_name = $this->translate(strtolower($display_name), PublicationSubUsage::DISPLAY_NAME_LANG_MODULE);
if (strpos($translated_display_name, 'MISSING') === false) {
$display_name = $translated_display_name;
}
$display_name = $this->getLocaleString(strtolower($display_name),
PublicationSubUsage::DISPLAY_NAME_LANG_MODULE, $display_name);
}
return trim($display_name);
}
Expand All @@ -40,7 +38,7 @@ public function getDisplayName(int $sub_id): string
public function generateTitle(string $parent_usage_id, string $title_text): string
{
$count_subs = PublicationSubUsage::where(['parent_usage_id' => $parent_usage_id])->count();
return $title_text . " (" . $this->translate('publication_usage_sub') . "-" . ($count_subs + 1) . ")";
return $title_text . " (" . $this->getLocaleString('publication_usage_sub') . "-" . ($count_subs + 1) . ")";
}

/**
Expand Down
32 changes: 0 additions & 32 deletions src/Model/Publication/Config/PublicationUsageGroupRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,13 @@

namespace srag\Plugins\Opencast\Model\Publication\Config;

use srag\Plugins\Opencast\LegacyHelpers\TranslatorTrait;

/**
* Class PublicationUsageGroupRepository
*
* @author Farbod Zamani Boroujeni <zamani@elan-ev.de>
*/
class PublicationUsageGroupRepository
{
use TranslatorTrait;
/**
* Returns the display name of the group but by looking for a record also in localization.
* @param int $group_id
* @return string
*/
public static function getDisplayName(int $group_id): string
{
$display_name = '';
$group = PublicationUsageGroup::find($group_id);
if (!empty($group) && !empty($group->getDisplayName())) {
$display_name = $group->getDisplayName();
$display_name = self::getLocalizedDisplayName($display_name);
}
return trim($display_name);
}

/**
* @param string $group_display_name
* @return string
*/
public static function getLocalizedDisplayName($group_display_name): string
{
$localized_display_name = self::translate(strtolower($group_display_name), PublicationUsageGroup::DISPLAY_NAME_LANG_MODULE);
if (strpos($localized_display_name, 'MISSING') === false) {
$group_display_name = $localized_display_name;
}
return trim($group_display_name);
}

/**
* @param array $sub_ids
* @return array
Expand Down
15 changes: 6 additions & 9 deletions src/Model/Publication/Config/PublicationUsageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace srag\Plugins\Opencast\Model\Publication\Config;

use srag\Plugins\Opencast\LegacyHelpers\TranslatorTrait;
use srag\Plugins\Opencast\Util\Locale\LocaleTrait;

/**
* Class PublicationUsageRepository
Expand All @@ -13,7 +13,7 @@
*/
class PublicationUsageRepository
{
use TranslatorTrait;
use LocaleTrait;
public function exists(string $usage): bool
{
return !is_null(PublicationUsage::find($usage));
Expand Down Expand Up @@ -104,14 +104,11 @@ public function store(
*/
public function getDisplayName(string $usage): string
{
$display_name = '';
$usage = $this->getUsage($usage);
if (!empty($usage->getDisplayName())) {
$display_name = $usage->getDisplayName();
$translated_display_name = $this->translate(strtolower($display_name), PublicationUsage::DISPLAY_NAME_LANG_MODULE);
if (strpos($translated_display_name, 'MISSING') === false) {
$display_name = $translated_display_name;
}
$display_name = $usage->getDisplayName() ?? '';
if (!empty($display_name)) {
$display_name = $this->getLocaleString(strtolower($display_name),
PublicationUsage::DISPLAY_NAME_LANG_MODULE, $display_name);
}
return trim($display_name);
}
Expand Down
47 changes: 47 additions & 0 deletions src/Util/Locale/LocaleTraits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);

namespace srag\Plugins\Opencast\Util\Locale;

/**
* Trait LocaleTrait
*
* @author Farbod Zamani Boroujeni <zamani@elan-ev.de>
*/
trait LocaleTrait
{
/**
* A translator function, which makes sure that strings go through pluign->txt method to get translated.
* It uses the plugin property of the class or gets it from global container.
* @param string $string string to translate
* @param string $module
* @param ?string $fallback the fallback string in case the translataion does not exist.
*
* @return string
*/
public function getLocaleString(string $string, ?string $module = '', ?string $fallback = null): string
{
$locale_string = $string;
// Attaching module if any!
if (!empty($module)) {
$locale_string = $module . "_" . $string;
}

// Make sure plugin object is there!
global $opencastContainer;
$plugin = property_exists($this, 'plugin') ? $this->plugin : $opencastContainer[\ilOpenCastPlugin::class];

// Performing the regular txt translation.
$translation = $plugin->txt($locale_string);

// If a fallback string is provided, we check for every scenario in which the txt method might return a value in case of not finding the original string. (backwards compatibility with ILIAS < 8)
$start = substr($translation, 0, 1);
$end = substr($translation, -1);
if (((empty($translation) || $translation === "MISSING" || strpos($translation, "MISSING ") === 0) ||
($start == '-' && $end == '-' && strpos($translation, $locale_string) !== false)) && !empty($fallback)) {
return $fallback;
}

return $translation;
}
}
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@
'srag\\Plugins\\Opencast\\Util\\FileTransfer\\OpencastIngestService' => $baseDir . '/src/Util/FileTransfer/OpencastIngestService.php',
'srag\\Plugins\\Opencast\\Util\\FileTransfer\\PaellaConfigStorageService' => $baseDir . '/src/Util/FileTransfer/PaellaConfigStorageService.php',
'srag\\Plugins\\Opencast\\Util\\FileTransfer\\UploadStorageService' => $baseDir . '/src/Util/FileTransfer/UploadStorageService.php',
'srag\\Plugins\\Opencast\\Util\\Locale\\LocaleTrait' => $baseDir . '/src/Util/Locale/LocaleTraits.php',
'srag\\Plugins\\Opencast\\Util\\Player\\LivePlayerDataBuilder' => $baseDir . '/src/Util/Player/LivePlayerDataBuilder.php',
'srag\\Plugins\\Opencast\\Util\\Player\\PaellaConfigService' => $baseDir . '/src/Util/Player/PaellaConfigService.php',
'srag\\Plugins\\Opencast\\Util\\Player\\PaellaConfigServiceFactory' => $baseDir . '/src/Util/Player/PaellaConfigServiceFactory.php',
Expand Down
35 changes: 18 additions & 17 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ class ComposerStaticInitf618280b835f1c6e99e4eb8d8a3e04f0
);

public static $prefixLengthsPsr4 = array (
's' =>
's' =>
array (
'srag\\LibraryLanguageInstaller\\OpenCast\\' => 39,
'srag\\LibrariesNamespaceChanger\\' => 31,
'srag\\DataTableUI\\OpenCast\\' => 26,
'srag\\DIC\\OpenCast\\' => 18,
'srag\\CustomInputGUIs\\OpenCast\\' => 30,
),
'P' =>
'P' =>
array (
'Psr\\Http\\Message\\' => 17,
'Psr\\Http\\Client\\' => 16,
),
'O' =>
'O' =>
array (
'OpencastApi\\Rest\\' => 17,
'OpencastApi\\Mock\\' => 17,
'OpencastApi\\' => 12,
),
'G' =>
'G' =>
array (
'GuzzleHttp\\Psr7\\' => 16,
'GuzzleHttp\\Promise\\' => 19,
Expand All @@ -42,56 +42,56 @@ class ComposerStaticInitf618280b835f1c6e99e4eb8d8a3e04f0
);

public static $prefixDirsPsr4 = array (
'srag\\LibraryLanguageInstaller\\OpenCast\\' =>
'srag\\LibraryLanguageInstaller\\OpenCast\\' =>
array (
0 => __DIR__ . '/..' . '/srag/librarylanguageinstaller/src',
),
'srag\\LibrariesNamespaceChanger\\' =>
'srag\\LibrariesNamespaceChanger\\' =>
array (
0 => __DIR__ . '/..' . '/srag/librariesnamespacechanger/src',
),
'srag\\DataTableUI\\OpenCast\\' =>
'srag\\DataTableUI\\OpenCast\\' =>
array (
0 => __DIR__ . '/..' . '/srag/datatable/src',
),
'srag\\DIC\\OpenCast\\' =>
'srag\\DIC\\OpenCast\\' =>
array (
0 => __DIR__ . '/..' . '/srag/dic/src',
),
'srag\\CustomInputGUIs\\OpenCast\\' =>
'srag\\CustomInputGUIs\\OpenCast\\' =>
array (
0 => __DIR__ . '/..' . '/srag/custominputguis/src',
),
'Psr\\Http\\Message\\' =>
'Psr\\Http\\Message\\' =>
array (
0 => __DIR__ . '/..' . '/psr/http-factory/src',
1 => __DIR__ . '/..' . '/psr/http-message/src',
),
'Psr\\Http\\Client\\' =>
'Psr\\Http\\Client\\' =>
array (
0 => __DIR__ . '/..' . '/psr/http-client/src',
),
'OpencastApi\\Rest\\' =>
'OpencastApi\\Rest\\' =>
array (
0 => __DIR__ . '/..' . '/elan-ev/opencast-api/src/OpencastApi/Rest',
),
'OpencastApi\\Mock\\' =>
'OpencastApi\\Mock\\' =>
array (
0 => __DIR__ . '/..' . '/elan-ev/opencast-api/src/OpencastApi/Mock',
),
'OpencastApi\\' =>
'OpencastApi\\' =>
array (
0 => __DIR__ . '/..' . '/elan-ev/opencast-api/src/OpencastApi',
),
'GuzzleHttp\\Psr7\\' =>
'GuzzleHttp\\Psr7\\' =>
array (
0 => __DIR__ . '/..' . '/guzzlehttp/psr7/src',
),
'GuzzleHttp\\Promise\\' =>
'GuzzleHttp\\Promise\\' =>
array (
0 => __DIR__ . '/..' . '/guzzlehttp/promises/src',
),
'GuzzleHttp\\' =>
'GuzzleHttp\\' =>
array (
0 => __DIR__ . '/..' . '/guzzlehttp/guzzle/src',
),
Expand Down Expand Up @@ -401,6 +401,7 @@ class ComposerStaticInitf618280b835f1c6e99e4eb8d8a3e04f0
'srag\\Plugins\\Opencast\\Util\\FileTransfer\\OpencastIngestService' => __DIR__ . '/../..' . '/src/Util/FileTransfer/OpencastIngestService.php',
'srag\\Plugins\\Opencast\\Util\\FileTransfer\\PaellaConfigStorageService' => __DIR__ . '/../..' . '/src/Util/FileTransfer/PaellaConfigStorageService.php',
'srag\\Plugins\\Opencast\\Util\\FileTransfer\\UploadStorageService' => __DIR__ . '/../..' . '/src/Util/FileTransfer/UploadStorageService.php',
'srag\\Plugins\\Opencast\\Util\\Locale\\LocaleTrait' => __DIR__ . '/../..' . '/src/Util/Locale/LocaleTraits.php',
'srag\\Plugins\\Opencast\\Util\\Player\\LivePlayerDataBuilder' => __DIR__ . '/../..' . '/src/Util/Player/LivePlayerDataBuilder.php',
'srag\\Plugins\\Opencast\\Util\\Player\\PaellaConfigService' => __DIR__ . '/../..' . '/src/Util/Player/PaellaConfigService.php',
'srag\\Plugins\\Opencast\\Util\\Player\\PaellaConfigServiceFactory' => __DIR__ . '/../..' . '/src/Util/Player/PaellaConfigServiceFactory.php',
Expand Down

0 comments on commit 058c399

Please sign in to comment.