rocklobster-in / bogo

This is the development repository for Bogo, a straight-forward multilingual plugin for WordPress.
https://wordpress.org/plugins/bogo/
Other
23 stars 14 forks source link

Dropdown Language Switcher #275

Open wolke7net opened 1 year ago

wolke7net commented 1 year ago

Create a shortcode named [bogo-dropdown]

add_shortcode( 'bogo-dropdown', 'my_bogo_shortcode' );

function my_bogo_shortcode( $atts ) {
  if( !function_exists( 'bogo_language_switcher_links' ) ) { return; }

  $links = bogo_language_switcher_links( $args );
  $count = 0;
  $output = "<select onchange='this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);' >";

  foreach( $links as $link ) {
    // abort if empty href
    if(empty( $link['href'] )) { continue; }

    $extra = '';
    if ( get_locale() === $link['locale'] ) {
      $extra = 'selected disabled';
    }

    $output .= "<option value='{$link['href']}' {$extra}> {$link['title']} </option>";
    $count++;
  }

  $output .= "</select>";

  // if only 1 language, output nothing
  if( $count <= 1 ) {
    return '';
  }

  return $output;
}
takayukister commented 1 year ago

Nice addition, but I think this should be rather added as a "view" of the existing language switcher, like [bogo view="menu"].

davidovic999 commented 4 months ago

Thanks for the Dropdown Language Switcher snippet. It works great! But I have a comment. The language names in the dropdown menu are translated into English. They are not displayed in their native language names as [Bogo] does. For example: Español, Français are translated into Spanish, French How can I get the [bogo-dropdown] shortcode to display native language names?