WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
Custom walker/more options for separate language switcher (6 posts)

  1. W00tgnome
    Member
    Posted 1 year ago #

    I am trying to add a separate language switcher using pll_the_languages().

    As far as I can tell it's not possible to customize things such as classes and title attributes for link text (outside of pll_flag_title and pll_the_languages).

    Because of this I tried adding a language switcher menu using register_nav_menus() and a corresponding language switcher menu in Appearance -> Menus. I associated all translations for the language switcher with this menu and a custom walker specified using wp_nav_menu().

    The problem with this approach is that WP ignores menus with 0 items (since 3.5) and I can't seem to find a setting to disable this behavior. That aside it also seems that the language switcher items are not modified by the walker function (classes from the switcher are still added for instance).

    Currently I've resorted to regex replacing inside pll_the_languages, but I find this approach pretty hacky and would like to request either a custom walker or a function where it is possible to get the language items and build the HTML from these.

    http://wordpress.org/extend/plugins/polylang/

  2. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Yes. I know all this is far from perfect. At least the filter pll_the_languages allows you to modify or completely overwrite the html ouptput.

    I planned to improve this in the future (though not in v1.0). Could you explain exactly what is your goal?

  3. W00tgnome
    Member
    Posted 1 year ago #

    Basically change:

    <ul class="language">
      <li class='lang-item lang-item-4 current-lang'>
        <a hreflang='en' href='/en/'>English</a>
      </li>
      <li class='lang-item lang-item-5'>
        <a hreflang='da' href='/da/'>Dansk</a>
      </li>
      <li class='lang-item lang-item-3'>
        <a hreflang='sv' href='/sv/'>Svenska</a>
      </li>
    </ul>

    To:

    <ul class="language">
      <li class="active">
        <a title="English" hreflang="en" class="en" href="/en/">English</a>
      </li>
      <li >
        <a title="Dansk" hreflang="da" class="da" href="/da/">Dansk</a>
      </li>
      <li >
        <a title="Svenska" hreflang="sv" class="sv" href="/sv/">Svenska</a>
      </li>
    </ul>

    In other words:
    - Replace single quotes with double quotes.
    - Apply class changes to <li> elements.
    - Apply class changes to <a> elements.
    - Add title attribute to <a> elements.

    I don't know how much work there is in creating the possibility for a custom walker to be registered, but personally I'd be happy with just an array list of elements and their respective data, such as ISO-639-1 language code, translated language name and whether or not it is the current language (and I think this would also be faster to implement).

  4. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Would something like http://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/ satisfy you?

    If yes, it's already available in the current development version (as support for the WPML API).

    http://downloads.wordpress.org/plugin/polylang.zip

  5. W00tgnome
    Member
    Posted 1 year ago #

    Yes, I think that would suffice.

    How does this work with Polylang? I take it instaling WPML is a neccessity?

  6. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    No need for WPML (trying to install two multilingual plugins will probably result in an horrible mess). Just call for the function icl_get_languages the same way it is described in the WPML documentation. And you should get the same results with the current development version of Polylang (with the exception of the English language name which does not exist in Polylang)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic