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

Weather widget: changing the language doesn't affect the whole template #2187

Merged
Show file tree
Hide file tree
Changes from 3 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
44 changes: 44 additions & 0 deletions lib/Helper/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,50 @@ public static function InitLocale($config, $language = NULL)
self::$jsLocaleRequested = str_replace('_', '-', self::$requestedLanguage);
}

/**
* Get translations for user selected language
* @param $config
* @param $language
* @return Translator|null
*/
public static function getTranslationsFromLocale($config, $language): ?Translator
dasgarner marked this conversation as resolved.
Show resolved Hide resolved
{
// The default language
$default = ($language === null) ? $config->getSetting('DEFAULT_LANGUAGE') : $language;

// Build an array of supported languages
$localeDir = PROJECT_ROOT . '/locale';
$supportedLanguages = array_map('basename', glob($localeDir . '/*.mo'));

// Record any matching languages we find.
$foundLanguage = null;

// Try to get the local firstly from _REQUEST (post then get)
if ($language != null) {
$parsedLanguage = str_replace('-', '_', $language);

// Check its valid
if (in_array($parsedLanguage . '.mo', $supportedLanguages)) {
$foundLanguage = $parsedLanguage;
}
}

// Are we still empty, then return null
if ($foundLanguage == '') {
dasgarner marked this conversation as resolved.
Show resolved Hide resolved
// Check the default
if (!in_array($default . '.mo', $supportedLanguages)) {
return null;
}
}

// Load translations
$translator = new Translator();
$translator->loadTranslations(Translations::fromMoFile($localeDir . '/' . $foundLanguage . '.mo'));
$translator->register();

return $translator;
}

/**
* Get the Locale
* @param null $characters The number of characters to take from the beginning of the local string
Expand Down
38 changes: 29 additions & 9 deletions lib/Widget/Render/WidgetHtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,21 @@ private function render(
$widget->isValid = 0;
}

$moduleLanguage = null;
$translator = null;

// Check if a language property is defined against the module
// Note: We are using the language defined against the module and not from the module template
foreach ($module->properties as $property) {
if ($property->id === 'lang' && !empty($property->value)) {
dasgarner marked this conversation as resolved.
Show resolved Hide resolved
$moduleLanguage = $property->value;
}
}

if ($moduleLanguage !== null) {
$translator = Translate::getTranslationsFromLocale($this->config, $moduleLanguage);
}

// Output some sample data and a data url.
$widgetData = [
'widgetId' => $widget->widgetId,
Expand Down Expand Up @@ -535,7 +550,7 @@ private function render(
if ($moduleTemplate->stencil !== null) {
if ($moduleTemplate->stencil->twig !== null) {
$twig['twig'][] = $this->twig->fetchFromString(
$this->decorateTranslations($moduleTemplate->stencil->twig),
$this->decorateTranslations($moduleTemplate->stencil->twig, $translator),
$widgetData['templateProperties'],
);
}
Expand Down Expand Up @@ -566,22 +581,22 @@ private function render(

if ($module->stencil->twig !== null) {
$twig['twig'][] = $this->twig->fetchFromString(
$this->decorateTranslations($module->stencil->twig),
$this->decorateTranslations($module->stencil->twig, null),
array_merge($modulePropertyValues, ['settings' => $module->getSettingsForOutput()]),
);
}
if ($module->stencil->hbs !== null) {
$twig['hbs']['module'] = [
'content' => $this->decorateTranslations($module->stencil->hbs),
'content' => $this->decorateTranslations($module->stencil->hbs, null),
'width' => $module->stencil->width,
'height' => $module->stencil->height,
'gapBetweenHbs' => $module->stencil->gapBetweenHbs,
];
}
if ($module->stencil->head !== null) {
$twig['head'][] = $this->twig->fetchFromString(
$this->decorateTranslations($module->stencil->head),
$modulePropertyValues
$this->decorateTranslations($module->stencil->head, null),
$modulePropertyValues,
);
}
if ($module->stencil->style !== null) {
Expand Down Expand Up @@ -663,7 +678,7 @@ private function render(

// Output the hbs
$twig['hbs'][$moduleTemplate->templateId] = [
'content' => $this->decorateTranslations($moduleTemplate->stencil->hbs),
'content' => $this->decorateTranslations($moduleTemplate->stencil->hbs, null),
'width' => $moduleTemplate->stencil->width,
'height' => $moduleTemplate->stencil->height,
'gapBetweenHbs' => $moduleTemplate->stencil->gapBetweenHbs,
Expand All @@ -676,7 +691,7 @@ private function render(
} else if ($extension !== null) {
// Output the extension HBS instead
$twig['hbs'][$moduleTemplate->templateId] = [
'content' => $this->decorateTranslations($extension->stencil->hbs),
'content' => $this->decorateTranslations($extension->stencil->hbs, null),
'width' => $extension->stencil->width,
'height' => $extension->stencil->height,
'gapBetweenHbs' => $extension->stencil->gapBetweenHbs,
Expand Down Expand Up @@ -743,9 +758,10 @@ private function render(
/**
* Decorate translations in template files.
* @param string $content
* @param \GetText\Translator $translator
* @return string
*/
private function decorateTranslations(string $content): string
private function decorateTranslations(string $content, ?\Gettext\Translator $translator): string
{
$matches = [];
preg_match_all('/\|\|.*?\|\|/', $content, $matches);
Expand All @@ -754,7 +770,11 @@ private function decorateTranslations(string $content): string
$translateTag = str_replace('||', '', $sub);

// We have a valid translateTag to substitute
$replace = __($translateTag);
if ($translator !== null) {
$replace = $translator->gettext($translateTag);
} else {
$replace = __($translateTag);
}

// Substitute the replacement we have found (it might be '')
$content = str_replace($sub, $replace, $content);
Expand Down
Loading