From 0a25a8067fae028a054dd585882619930a113d44 Mon Sep 17 00:00:00 2001 From: Pedro DELGADO ALCARAZ Date: Thu, 17 Oct 2024 16:41:19 +0200 Subject: [PATCH 1/3] Adding the capability of translating files by append language code. --- lang/en/local_staticpage.php | 1 + settings_pagelist.php | 28 ++++++++++++++++++++++++++++ view.php | 12 ++++++++++++ 3 files changed, 41 insertions(+) diff --git a/lang/en/local_staticpage.php b/lang/en/local_staticpage.php index a08b51c..cd320db 100644 --- a/lang/en/local_staticpage.php +++ b/lang/en/local_staticpage.php @@ -65,6 +65,7 @@ $string['settingspagelist'] = 'List of static pages'; $string['settingspagelistentryfilename'] = 'The following document file was found:
{$a}'; $string['settingspagelistentrypagename'] = 'From the document file\'s filename, Moodle derived the following pagename:
{$a}'; +$string['settingspagelistentrypagelanguages'] = 'Translations found:
{$a}'; $string['settingspagelistentryrewritedisabled'] = 'The static page should be available at the following clean URL, but is not verified because checking availability is disabled:
{$a}'; $string['settingspagelistentryrewriteerror'] = 'The static page should be available at the following clean URL, but actually a browser won\'t be able to download and view it either because of connection error or responding slower than checkavailabilitytimeout config (perhaps there is something wrong with your webserver or mod_rewrite configuration):
{$a}'; $string['settingspagelistentryrewritefail'] = 'The static page should be available at the following clean URL, but actually a browser won\'t be able to download and view it due to a non-2xx HTTP status code (perhaps there is something wrong with your webserver or mod_rewrite configuration):
{$a}'; diff --git a/settings_pagelist.php b/settings_pagelist.php index e1a4d37..17a7ed9 100644 --- a/settings_pagelist.php +++ b/settings_pagelist.php @@ -81,6 +81,30 @@ // Get plugin config. $localstaticpageconfig = get_config('local_staticpage'); + // Initialize page and language variables + $pageLanguages = []; + $pageName = ''; + + // Preprocess $pages array to remove translations from the list + foreach ($pages as $key => $page) { + + // Collect information about the page. + $fileName = $page->get_filename(); + $pageName = pathinfo($fileName, PATHINFO_FILENAME); + + // Check if the page is a translation. + if (preg_match('/--[a-zA-Z]{2}$/', $pageName, $matches)) { + // Get the language code. + $language = substr($matches[0], 2); + // Get the default page name + $defaultPageName = substr($pageName, 0, -4); + // Add the language to the list. + $pageLanguages[$defaultPageName][] = $language; + // Remove the translation from the list + unset($pages[$key]); + } + } + // Output each page as a page list entry. foreach ($pages as $page) { @@ -101,6 +125,10 @@ $html .= html_writer::tag('p', get_string('settingspagelistentryfilename', 'local_staticpage', $pagefilename)); $html .= html_writer::tag('p', get_string('settingspagelistentrypagename', 'local_staticpage', $pagepagename)); + if (array_key_exists($pagepagename, $pageLanguages)) { + $html .= html_writer::tag('p', get_string('settingspagelistentrypagelanguages', 'local_staticpage', implode(', ', $pageLanguages[$pagepagename]))); + } + // Print normal static page URL - Do only if apache rewrite isn't forced. if (!$localstaticpageconfig->apacherewrite) { // Check availability. diff --git a/view.php b/view.php index 1d78b8a..8a2a56f 100644 --- a/view.php +++ b/view.php @@ -66,6 +66,18 @@ // Get filearea. $fs = get_file_storage(); +// Get current language +$lang = current_language(); + +// Get translated file +$filename_traslated = "$page--$lang.html"; +$file = $fs->get_file($context->id, 'local_staticpage', 'documents', 0, '/', $filename_traslated); + +// If translated file exists replace default filename +if ($file) { + $filename = $filename_traslated; +} + // Get document from filearea. $file = $fs->get_file($context->id, 'local_staticpage', 'documents', 0, '/', $filename); From c2a398ab426e28f9ea64a44e134810844723ff9f Mon Sep 17 00:00:00 2001 From: Pedro DELGADO ALCARAZ Date: Thu, 7 Nov 2024 20:05:10 +0100 Subject: [PATCH 2/3] Moodle CI checks corrections. --- lang/en/local_staticpage.php | 2 +- settings_pagelist.php | 26 +++++++++++++------------- view.php | 12 ++++++------ 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lang/en/local_staticpage.php b/lang/en/local_staticpage.php index cd320db..cdaa340 100644 --- a/lang/en/local_staticpage.php +++ b/lang/en/local_staticpage.php @@ -64,8 +64,8 @@ $string['processfiltersyes'] = 'Yes, process filters'; $string['settingspagelist'] = 'List of static pages'; $string['settingspagelistentryfilename'] = 'The following document file was found:
{$a}'; -$string['settingspagelistentrypagename'] = 'From the document file\'s filename, Moodle derived the following pagename:
{$a}'; $string['settingspagelistentrypagelanguages'] = 'Translations found:
{$a}'; +$string['settingspagelistentrypagename'] = 'From the document file\'s filename, Moodle derived the following pagename:
{$a}'; $string['settingspagelistentryrewritedisabled'] = 'The static page should be available at the following clean URL, but is not verified because checking availability is disabled:
{$a}'; $string['settingspagelistentryrewriteerror'] = 'The static page should be available at the following clean URL, but actually a browser won\'t be able to download and view it either because of connection error or responding slower than checkavailabilitytimeout config (perhaps there is something wrong with your webserver or mod_rewrite configuration):
{$a}'; $string['settingspagelistentryrewritefail'] = 'The static page should be available at the following clean URL, but actually a browser won\'t be able to download and view it due to a non-2xx HTTP status code (perhaps there is something wrong with your webserver or mod_rewrite configuration):
{$a}'; diff --git a/settings_pagelist.php b/settings_pagelist.php index a486c09..df7f77b 100644 --- a/settings_pagelist.php +++ b/settings_pagelist.php @@ -83,26 +83,26 @@ // Get plugin config. $localstaticpageconfig = get_config('local_staticpage'); - // Initialize page and language variables - $pageLanguages = []; - $pageName = ''; + // Initialize page and language variables. + $pagelanguages = []; + $pagename = ''; - // Preprocess $pages array to remove translations from the list + // Preprocess $pages array to remove translations from the list. foreach ($pages as $key => $page) { // Collect information about the page. - $fileName = $page->get_filename(); - $pageName = pathinfo($fileName, PATHINFO_FILENAME); + $filename = $page->get_filename(); + $pagename = pathinfo($filename, PATHINFO_FILENAME); // Check if the page is a translation. - if (preg_match('/--[a-zA-Z]{2}$/', $pageName, $matches)) { + if (preg_match('/--[a-zA-Z]{2}$/', $pagename, $matches)) { // Get the language code. $language = substr($matches[0], 2); - // Get the default page name - $defaultPageName = substr($pageName, 0, -4); + // Get the default page name. + $defaultpagename = substr($pagename, 0, -4); // Add the language to the list. - $pageLanguages[$defaultPageName][] = $language; - // Remove the translation from the list + $pagelanguages[$defaultpagename][] = $language; + // Remove the translation from the list. unset($pages[$key]); } } @@ -127,8 +127,8 @@ $html .= html_writer::tag('p', get_string('settingspagelistentryfilename', 'local_staticpage', $pagefilename)); $html .= html_writer::tag('p', get_string('settingspagelistentrypagename', 'local_staticpage', $pagepagename)); - if (array_key_exists($pagepagename, $pageLanguages)) { - $html .= html_writer::tag('p', get_string('settingspagelistentrypagelanguages', 'local_staticpage', implode(', ', $pageLanguages[$pagepagename]))); + if (array_key_exists($pagepagename, $pagelanguages)) { + $html .= html_writer::tag('p', get_string('settingspagelistentrypagelanguages', 'local_staticpage', implode(', ', $pagelanguages[$pagepagename]))); } // Print normal static page URL - Do only if apache rewrite isn't forced. diff --git a/view.php b/view.php index 8a2a56f..af75671 100644 --- a/view.php +++ b/view.php @@ -66,16 +66,16 @@ // Get filearea. $fs = get_file_storage(); -// Get current language +// Get current language. $lang = current_language(); -// Get translated file -$filename_traslated = "$page--$lang.html"; -$file = $fs->get_file($context->id, 'local_staticpage', 'documents', 0, '/', $filename_traslated); +// Get translated file. +$filenametranslated = "$page--$lang.html"; +$file = $fs->get_file($context->id, 'local_staticpage', 'documents', 0, '/', $filenametranslated); -// If translated file exists replace default filename +// If translated file exists replace default filename. if ($file) { - $filename = $filename_traslated; + $filename = $filenametranslated; } // Get document from filearea. From 717fedf2591f82056cf39ff48cf41afbb0138b48 Mon Sep 17 00:00:00 2001 From: Pedro Delgado Date: Thu, 7 Nov 2024 20:27:07 +0100 Subject: [PATCH 3/3] Too long statement fix (#1) Co-authored-by: Pedro DELGADO ALCARAZ --- settings_pagelist.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/settings_pagelist.php b/settings_pagelist.php index df7f77b..0ae5021 100644 --- a/settings_pagelist.php +++ b/settings_pagelist.php @@ -128,7 +128,8 @@ $html .= html_writer::tag('p', get_string('settingspagelistentrypagename', 'local_staticpage', $pagepagename)); if (array_key_exists($pagepagename, $pagelanguages)) { - $html .= html_writer::tag('p', get_string('settingspagelistentrypagelanguages', 'local_staticpage', implode(', ', $pagelanguages[$pagepagename]))); + $html .= html_writer::tag('p', get_string('settingspagelistentrypagelanguages', 'local_staticpage', + implode(', ', $pagelanguages[$pagepagename]))); } // Print normal static page URL - Do only if apache rewrite isn't forced.