Skip to content

Commit

Permalink
Add setting to show menu items in alternative language (#1063)
Browse files Browse the repository at this point in the history
Use case: not all menus are translated for the current language.
Allow menus to be shown in the first available language.
False by default = menus require translation to be shown.
  • Loading branch information
cdauth committed Oct 25, 2022
1 parent 821e94b commit b74ca9f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
7 changes: 7 additions & 0 deletions admin/qtx_admin_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,13 @@ private function add_general_section() {
<?php _e( 'The message about available languages for the content of a post or a page may also appear if a single post display with an untranslated content if viewed directly.', 'qtranslate' ) ?>
<?php printf( __( 'This function will not work correctly if you installed %s on a blog with existing entries. In this case you will need to take a look at option "%s" under "%s" section.', 'qtranslate' ), 'qTranslate', __( 'Convert Database', 'qtranslate' ), __( 'Import', 'qtranslate' ) . '/' . __( 'Export', 'qtranslate' ) ) ?></p>
<br/>
<label for="show_menu_alternative_language">
<input type="checkbox" name="show_menu_alternative_language" id="show_menu_alternative_language"
value="1"<?php checked( $q_config['show_menu_alternative_language'] ) ?>/> <?php _e( 'Show menu items in an alternative language when translation is not available for the selected language.', 'qtranslate' ) ?>
</label>
<br/>
<p class="qtranxs-notes"><?php printf( __( 'When checked, menus are shown in the first available language ordered as defined by option "%s". If unchecked, menus are displayed only if translated in the current language.', 'qtranslate' ), __( 'Default Language / Order', 'qtranslate' ) ) ?></p>
<br/>
<label for="show_alternative_content">
<input type="checkbox"
name="show_alternative_content"
Expand Down
15 changes: 9 additions & 6 deletions qtranslate_frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,16 @@ function qtranxf_wp_get_nav_menu_items( $items, $menu, $args ) {
$item_title = $item->title;
if ( ! empty( $item_title ) ) {
if ( empty( $item->post_title ) && ! qtranxf_isMultilingual( $item_title ) ) {
//$item does not have custom menu title, then it fetches information from post_title, but it is already translated with ShowEmpty=false, which gives either valid translation or possibly something like "(English) English Text". We need to translate again and to skip menu item if translation does not exist.
// Item does not have custom menu title.
switch ( $item->type ) {
case 'post_type':
$p = get_post( $item->object_id );
if ( $p ) {
$post_title_ml = isset( $p->post_title_ml ) ? $p->post_title_ml : $p->post_title;
$item_title = qtranxf_use_language( $language, $post_title_ml, false, true );
// Fetch information from post_title, but it's already translated with ShowEmpty=false,
// which gives either valid translation or possibly something like "(English) English Text".
// Translate again and skip menu item if translation does not exist.
$post = get_post( $item->object_id );
if ( $post ) {
$post_title_ml = isset( $post->post_title_ml ) ? $post->post_title_ml : $post->post_title;
$item_title = qtranxf_use_language( $language, $post_title_ml, false, ! $q_config['show_menu_alternative_language'] );
}
break;
case 'taxonomy':
Expand All @@ -120,7 +123,7 @@ function qtranxf_wp_get_nav_menu_items( $items, $menu, $args ) {
break;
}
} else {
$item_title = qtranxf_use_language( $language, $item_title, false, true );
$item_title = qtranxf_use_language( $language, $item_title, false, ! $q_config['show_menu_alternative_language'] );
}
}
if ( empty( $item_title ) ) {
Expand Down
1 change: 1 addition & 0 deletions qtranslate_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function qtranxf_set_default_options( &$ops ) {
$ops['front']['bool'] = array(
'detect_browser_language' => true, // enables browser language detection
'hide_untranslated' => false, // hide pages without content
'show_menu_alternative_language' => false, // hide menu items without a translation
'show_displayed_language_prefix' => true,
'show_alternative_content' => false,
'hide_default_language' => true, // hide language tag for default language in urls
Expand Down

0 comments on commit b74ca9f

Please sign in to comment.