diff options
author | Mario <mario@mariovavti.com> | 2024-07-29 09:59:04 +0000 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2024-07-29 09:59:04 +0000 |
commit | 52f7b508af7bd6eb00eb76b4e170b44a55be869f (patch) | |
tree | d72d94366a985be578cc4729fb9ac2078cbc267e | |
parent | 15f6bc93cf53cf05162d1314852f567ab2af2bc6 (diff) | |
parent | d3093dce1bfd4685d5e11f828689630862f77bda (diff) | |
download | volse-hubzilla-52f7b508af7bd6eb00eb76b4e170b44a55be869f.tar.gz volse-hubzilla-52f7b508af7bd6eb00eb76b4e170b44a55be869f.tar.bz2 volse-hubzilla-52f7b508af7bd6eb00eb76b4e170b44a55be869f.zip |
Merge branch 'improve-help-locale-handling' into 'dev'
Improve handling of locale in Help module
See merge request hubzilla/core!2140
-rw-r--r-- | Zotlabs/Lib/Traits/HelpHelperTrait.php | 47 | ||||
-rw-r--r-- | Zotlabs/Module/Help.php | 13 | ||||
-rw-r--r-- | Zotlabs/Widget/Helpindex.php | 4 | ||||
-rw-r--r-- | tests/unit/Module/HelpTest.php | 67 | ||||
-rw-r--r-- | view/js/mod_help.js | 50 | ||||
-rw-r--r-- | view/tpl/help.tpl | 21 |
6 files changed, 93 insertions, 109 deletions
diff --git a/Zotlabs/Lib/Traits/HelpHelperTrait.php b/Zotlabs/Lib/Traits/HelpHelperTrait.php index b7711bbd5..63b0eb22e 100644 --- a/Zotlabs/Lib/Traits/HelpHelperTrait.php +++ b/Zotlabs/Lib/Traits/HelpHelperTrait.php @@ -15,6 +15,15 @@ trait HelpHelperTrait { private string $file_type = ''; /** + * Associative array containing the detected language. + */ + private array $lang = [ + 'language' => 'en', //! Detected language, 2-letter ISO 639-1 code ("en") + 'from_url' => false, //! true if language from URL overrides browser default + 'missing' => false, //! true if topic not found in detected language + ]; + + /** * Determines help language. * * If the language was specified in the URL, override the language preference @@ -28,17 +37,15 @@ trait HelpHelperTrait { $languages = $language_repository->getList(); if(array_key_exists(argv(1), $languages)) { - $lang = argv(1); - $from_url = true; + $this->lang['language'] = argv(1); + $this->lang['from_url'] = true; } else { - $lang = \App::$language; - if(! isset($lang)) - $lang = 'en'; + if(isset(\App::$language)) { + $this->lang['language'] = \App::$language; + } - $from_url = false; + $this->lang['from_url'] = false; } - - $this->lang = array('language' => $lang, 'from_url' => $from_url); } /** @@ -53,10 +60,10 @@ trait HelpHelperTrait { // Use local variable until we can use trait constants. $valid_file_ext = ['md', 'bb', 'html']; - $base_path = "doc/{$lang}/${base_path}"; + $base_path_with_lang = "doc/{$lang}/${base_path}"; foreach ($valid_file_ext as $ext) { - $path = "{$base_path}.{$ext}"; + $path = "{$base_path_with_lang}.{$ext}"; if (file_exists($path)) { $this->file_name = $path; $this->file_type = $ext; @@ -64,5 +71,25 @@ trait HelpHelperTrait { break; } } + + if (empty($this->file_name) && $lang !== 'en') { + $this->lang['missing'] = true; + $this->find_help_file($base_path, 'en'); + } + } + + public function missing_translation(): bool { + return !!$this->lang['missing']; + } + + public function missing_translation_message(): string { + $prefered_language_name = get_language_name( + $this->lang['language'], + $this->lang['language'] + ); + + return bbcode( + t("This page is not yet available in {$prefered_language_name}. See [observer.baseurl]/help/developer/developer_guide#Translations for information about how to help.") + ); } } diff --git a/Zotlabs/Module/Help.php b/Zotlabs/Module/Help.php index fd5cacee6..851776591 100644 --- a/Zotlabs/Module/Help.php +++ b/Zotlabs/Module/Help.php @@ -6,6 +6,7 @@ use Michelf\MarkdownExtra; /** * You can create local site resources in doc/Site.md and either link to doc/Home.md for the standard resources * or use our include mechanism to include it on your local page. + * *@code * #include doc/Home.md; *@endcode @@ -19,14 +20,6 @@ class Help extends \Zotlabs\Web\Controller { private string $heading_slug = ''; /** - * Associative array containing the detected language. - */ - public array $lang = [ - 'language' => 'en', //! Detected language, 2-letter ISO 639-1 code ("en") - 'from_url' => false, //! true if language from URL overrides browser default - ]; - - /** * Pre-check before processing request. * * Determine language requested, and ensure that a topic was requested. @@ -160,6 +153,10 @@ class Help extends \Zotlabs\Web\Controller { array_shift($args); } + if (empty($args)) { + goaway("/help/{$this->lang['language']}/about/about"); + } + // Keep the first remaining arg as the heading slug $this->heading_slug = $args[0]; diff --git a/Zotlabs/Widget/Helpindex.php b/Zotlabs/Widget/Helpindex.php index 5264e1947..fd9204c9e 100644 --- a/Zotlabs/Widget/Helpindex.php +++ b/Zotlabs/Widget/Helpindex.php @@ -22,7 +22,9 @@ class Helpindex { $this->find_help_file('toc', $this->lang['language']); if (! empty($this->file_name)) { - $this->contents = file_get_contents($this->file_name); + $this->contents = translate_projectname( + file_get_contents($this->file_name) + ); } $tpl = get_markup_template('widget.tpl'); diff --git a/tests/unit/Module/HelpTest.php b/tests/unit/Module/HelpTest.php index c345d5e52..2c1d31570 100644 --- a/tests/unit/Module/HelpTest.php +++ b/tests/unit/Module/HelpTest.php @@ -31,30 +31,7 @@ class HelpTest extends \Zotlabs\Tests\Unit\Module\TestCase { * ["html"] */ public function test_get_request_when_help_file_exists(string $ext): void { - // Stub file exists, to only retur true for the file with the current - // extension - $fe_stub = $this->getFunctionMock('Zotlabs\Lib\Traits', 'file_exists'); - $fe_stub - ->expects($this->any()) - ->willReturnCallback( - fn (string $path) => $path === "doc/en/about/help_topic.{$ext}" - ); - - // Use a value map to make the `file_get_contents` stub return the - // correct content for the file types. - $file_content_map = [ - [ 'doc/en/about/help_topic.md', "### Help heading\n\$Projectname help content" ], - [ 'doc/en/about/help_topic.bb', "[h3]Help heading[/h3]\n\n\$Projectname help content" ], - [ 'doc/en/about/help_topic.html', "<h3>Help heading</h3><p>\$Projectname help content</p>" ], - ]; - - // Stub `file_get_contents` to plant our own content. - $fgc_stub = $this->getFunctionMock('Zotlabs\Module', 'file_get_contents'); - $fgc_stub - ->expects($this->once()) - ->willReturnMap($file_content_map); - - + $stubs = $this->prepare_stubs($ext); $this->get("help/about/help_topic"); // Check that markdown content was correctly rendered @@ -104,6 +81,11 @@ class HelpTest extends \Zotlabs\Tests\Unit\Module\TestCase { $this->get('help'); } + public function test_getting_locale_with_no_topic_should_redirect_to_about_page_for_locale(): void { + $this->expectRedirectTo('help/de/about/about'); + $this->get('help/de'); + } + public function test_find_help_file_returns_first_match(): void { // Stub file exists, to always return true $fe_stub = $this->getFunctionMock('Zotlabs\Lib\Traits', 'file_exists'); @@ -121,6 +103,16 @@ class HelpTest extends \Zotlabs\Tests\Unit\Module\TestCase { $this->get('help/first'); } + public function test_fall_back_to_english_if_localized_topic_dont_exist(): void { + \App::$language = 'nb'; + + $stubs = $this->prepare_stubs('bb'); + $this->get('help/about/help_topic'); + + $this->assertPageContains('Hubzilla Documentation: About'); + $this->assertPageContains('This page is not yet available in norsk bokmål'); + } + public function test_includes(): void { // Stub `file_get_contents` to plant our own content. $fgc_stub = $this->getFunctionMock('Zotlabs\Module', 'file_get_contents'); @@ -176,4 +168,31 @@ class HelpTest extends \Zotlabs\Tests\Unit\Module\TestCase { $this->assertPageContains('<h3>This is the included file.</h3>'); } + + private function prepare_stubs(string $ext): array { + // Stub file exists, to only retur true for the file with the current + // extension + $fe_stub = $this->getFunctionMock('Zotlabs\Lib\Traits', 'file_exists'); + $fe_stub + ->expects($this->any()) + ->willReturnCallback( + fn (string $path) => $path === "doc/en/about/help_topic.{$ext}" + ); + + // Use a value map to make the `file_get_contents` stub return the + // correct content for the file types. + $file_content_map = [ + [ 'doc/en/about/help_topic.md', "### Help heading\n\$Projectname help content" ], + [ 'doc/en/about/help_topic.bb', "[h3]Help heading[/h3]\n\n\$Projectname help content" ], + [ 'doc/en/about/help_topic.html', "<h3>Help heading</h3><p>\$Projectname help content</p>" ], + ]; + + // Stub `file_get_contents` to plant our own content. + $fgc_stub = $this->getFunctionMock('Zotlabs\Module', 'file_get_contents'); + $fgc_stub + ->expects($this->once()) + ->willReturnMap($file_content_map); + + return [ 'file_exists' => $fe_stub, 'file_get_contents' => $fgc_stub ]; + } } diff --git a/view/js/mod_help.js b/view/js/mod_help.js index 9c3591498..e2b1f4785 100644 --- a/view/js/mod_help.js +++ b/view/js/mod_help.js @@ -69,54 +69,4 @@ $(document).ready(function () { var newref = p.protocol + '//' + p.hostname + portstr + p.pathname + p.hash.split('?').shift(); location.replace(newref) } - - - // Determine language translations available from the language selector menu itself - var langChoices = []; - $('.lang-selector').find('.lang-choice').each(function (idx, a) { - langChoices.push($(a).html()); - }); - // Parse the URL and insert the language code for the loaded language, based - // on the variable "help_language" that is declared in the help.tpl page template - var path = window.location.pathname.split('/'); - var pathParts = []; - var pick_me = true; - for (var i = 0; i < path.length; i++) { - if(i === 2 && pick_me ) { - if(path[i].length > 0) { - pathParts.push(help_language); - pick_me = false; - if($.inArray(path[i], langChoices) < 0) { - i--; - } - } - } else { - if(path[i].length > 0) { - pathParts.push(path[i]); - } - } - - } - // Update the address bar to reflect the loaded language - window.history.replaceState({}, '', '/' + pathParts.join('/')); - - // Highlight the language in the language selector that is currently viewed - $('.lang-selector').find('.lang-choice:contains("' + help_language + '")').addClass('active'); - - // Construct the links to the available translations based and populate the selector menu - $('.lang-selector').find('.lang-choice').each(function (idx, a) { - var langLink = []; - - for (var i = 0; i < pathParts.length; i++) { - - if(i === 1) { - langLink.push($(a).html()); - } else { - langLink.push(pathParts[i]); - } - - } - $(a).attr('href', '/' + langLink.join('/')); - }); - }); diff --git a/view/tpl/help.tpl b/view/tpl/help.tpl index ba61a43ce..0dc9bc686 100644 --- a/view/tpl/help.tpl +++ b/view/tpl/help.tpl @@ -1,20 +1,12 @@ <div id="help-content" class="generic-content-wrapper"> <div class="clearfix section-title-wrapper"> - <div class="float-end"> - <div class="btn-group"> - <button type="button" class="btn btn-outline-secondary btn-sm dropdown-toggle" data-bs-toggle="dropdown"> - <i class="fa fa-language" style="font-size: 1.4em;"></i> - </button> - <div class="dropdown-menu dropdown-menu-end flex-column lang-selector"> - <a class="dropdown-item lang-choice" href="/help">de</a> - <a class="dropdown-item lang-choice" href="/help">en</a> - <a class="dropdown-item lang-choice" href="/help">es</a> - <a class="dropdown-item lang-choice" href="/help">fr</a> - </div> - </div> - </div> <h2>{{$module->get_page_title()}}</h2> </div> + {{if $module->missing_translation()}} + <div class="notice section-content-info-wrapper"> + {{$module->missing_translation_message()}} + </div> + {{/if}} <div class="section-content-wrapper" id="doco-content"> <h3 id="doco-top-toc-heading"> <span class="fakelink" onclick="docoTocToggle(); return false;"> @@ -26,6 +18,3 @@ {{$module->render_content()}} </div> </div> -<script> - var help_language = '{{$module->lang["language"]}}' -</script> |