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

Dropdown Language Switcher #275

Open
wolke7net opened this issue May 9, 2023 · 2 comments
Open

Dropdown Language Switcher #275

wolke7net opened this issue May 9, 2023 · 2 comments
Assignees

Comments

@wolke7net
Copy link

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
Copy link
Collaborator

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

@takayukister takayukister self-assigned this May 10, 2023
@takayukister takayukister added this to the 3.8 milestone May 10, 2023
@takayukister takayukister removed this from the 3.8 milestone Apr 29, 2024
@davidovic999
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants