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

Adding the capability of translating files by append language code. #75

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions lang/en/local_staticpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
$string['processfiltersyes'] = 'Yes, process filters';
$string['settingspagelist'] = 'List of static pages';
$string['settingspagelistentryfilename'] = 'The following document file was found:<br /><strong>{$a}</strong>';
$string['settingspagelistentrypagelanguages'] = 'Translations found:<br /><strong>{$a}</strong>';
$string['settingspagelistentrypagename'] = 'From the document file\'s filename, Moodle derived the following pagename:<br /><strong>{$a}</strong>';
$string['settingspagelistentryrewritedisabled'] = 'The static page should be available at the following clean URL, but is not verified because checking availability is disabled:<br /><strong>{$a}</strong>';
$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):<br /><strong>{$a}</strong>';
Expand Down
29 changes: 29 additions & 0 deletions settings_pagelist.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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) {

Expand All @@ -103,6 +127,11 @@
$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.
Expand Down
12 changes: 12 additions & 0 deletions view.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@
// Get filearea.
$fs = get_file_storage();

// Get current language.
$lang = current_language();

// 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 ($file) {
$filename = $filenametranslated;
}

// Get document from filearea.
$file = $fs->get_file($context->id, 'local_staticpage', 'documents', 0, '/', $filename);

Expand Down