Page 1 of 1

Possibility of adding multilingual navbar support?

Posted: Tue Apr 12, 2016 11:12 am
by wesley
Recently, I started using a theme that uses global navigation on my multilingual blog. One thing I would have liked to see was having an option to show different navbar items depending on the language selected. Basically, providing navbar items in appropriate languages.

Would it be possible to tack on a language selection box on the existing framework, or should I just do away with the global navigation and have a custom array just for the theme?

Re: Possibility of adding multilingual navbar support?

Posted: Tue Apr 12, 2016 12:20 pm
by garvinhicking
I believe adding a language selection checkbox that toggles additional input fields there would be quite complicated.

I'd rather suggest you create one additional menu item in the configuration for each language, and then depending on the language you later pass the corresponding menu item to the output.


HTh,
Garvin

Re: Possibility of adding multilingual navbar support?

Posted: Tue Apr 12, 2016 1:29 pm
by wesley
That sounds sensible. I'll give it a try. Thanks.

Re: Possibility of adding multilingual navbar support?

Posted: Tue Apr 12, 2016 7:14 pm
by wesley
Alright, I got it working! I made it so that you get a dropdown box for each navigation item's language. You can pick "All" or a specific language, like the current multilingual HTML nugget plugin.
multilingual_menu.png
multilingual_menu.png (46.02 KiB) Viewed 18343 times
My website http://tool-box.info/blog/ has five menu items assigned to each of the two languages it's in (English and Korean) for a total of ten. Switching the language will show the appropriate five.

Re: Possibility of adding multilingual navbar support?

Posted: Tue Apr 12, 2016 7:28 pm
by Timbalu
I wish your dancing panda could stand still. Its hard to concentrate with this up and down... :)

Maybe you can give a working code example for others to build this into their theme too?

Re: Possibility of adding multilingual navbar support?

Posted: Wed Apr 13, 2016 2:20 am
by wesley
It was a quick-and-dirty implementation involving adding of a variable into the existing array.

In the config.inc.php file, instead of calling out this function:

Code: Select all

serendipity_loadGlobalThemeOptions($template_config, $template_loaded_config, $template_global_config);
I added a block of code that does the same thing, plus accepting a language variable:

Code: Select all

$navlinks = array();
$conf_amount = array(
    'var'           => 'amount',
    'name'          => NAVLINK_AMOUNT,
    'type'          => 'string',
    'default'       => '5',
    'scope'         => 'global'
);
$template_config[] = $conf_amount;
if (!isset($template_loaded_config['amount']) || empty($template_loaded_config['amount'])) {
    $template_loaded_config['amount'] = $conf_amount['default'];
}

for ($i = 0; $i < $template_loaded_config['amount']; $i++) {
    $navlinks[] = array(
        'title' => $template_loaded_config['navlink' . $i . 'text'],
        'href'  => $template_loaded_config['navlink' . $i . 'url'],
        'language' => $template_loaded_config['navlink' . $i . 'lang']
    );
    $template_config[] = array(
        'var'           => 'navlink' . $i . 'text',
        'name'          => NAV_LINK_TEXT . ' #' . ($i+1),
        'type'          => 'string',
        'default'       => 'Link #' . ($i+1),
        'scope'         => 'global'
    );
    $template_config[] = array(
        'var'           => 'navlink' . $i . 'url',
        'name'          => NAV_LINK_URL . ' #' . ($i+1),
        'type'          => 'string',
        'default'       => '#',
        'scope'         => 'global'
    );
    $template_config[] = array(
        'var'           => 'navlink' . $i . 'lang',
        'name'          => INSTALL_LANG . ' #' . ($i+1),
        'type'          => 'select',
        'default'       => 'all',
        'select_values' => array('all' => COMMENTS_FILTER_ALL,
                    			 'en' => 'English',
                    			 'de' => 'German',
                    			 'da' => 'Danish',
                    			 'es' => 'Spanish',
                    			 'fr' => 'French',
                    			 'fi' => 'Finnish',
                    			 'cs' => 'Czech (Win-1250)',
                    			 'cz' => 'Czech (ISO-8859-2)',
                    			 'nl' => 'Dutch',
                    			 'is' => 'Icelandic',
                    			 'se' => 'Swedish',
                    			 'pt' => 'Portuguese Brazilian',
                    			 'pt_PT' => 'Portuguese European',
                    			 'bg' => 'Bulgarian',
                    			 'hu' => 'Hungarian',
                    			 'no' => 'Norwegian',
                    			 'ro' => 'Romanian',
                    			 'it' => 'Italian',
                    			 'ru' => 'Russian',
                    			 'fa' => 'Persian',
                    			 'tw' => 'Traditional Chinese (Big5)',
                    			 'tn' => 'Traditional Chinese (UTF-8)',
                    			 'zh' => 'Simplified Chinese (GB2312)',
                    			 'cn' => 'Simplified Chinese (UTF-8)',
                    			 'ja' => 'Japanese',
                    			 'ko' => 'Korean'),
        'scope'			=> 'global'
    );
}
$serendipity['smarty']->assignByRef('navlinks', $navlinks);
At first, I added the code for the language variable after calling out serendipity_loadGlobalThemeOptions, but then the dropdown box for the language appeared together at the bottom of the page, so I went with this way.

Then in the index.tpl of the theme, the navbar rendering implements "navlink.language" like so:

Code: Select all

<ul>
{foreach from=$navlinks item="navlink"}
    {if $navlink.language!='all'&&$navlink.language!=$lang}
    {elseif $navlink.title!=""&&$navlink.href!=""}
        <li>
        {if $currpage==$navlink.href or $currpage2==$navlink.href}
            <span>
        {else}
            <a href="{$navlink.href}">
        {/if}
        {$navlink.title}
        {if $currpage==$navlink.href or $currpage2==$navlink.href}
            </span>
        {else}
            </a>
        {/if}
        </li>
    {/if}
{/foreach}
</ul>
The initial {if}{elseif} should filter out a menu item if it's not assigned to the correct language.

P.S. The avatar is the dancing badger from the 2003 fiash animation that was quite famous back then. :)

Re: Possibility of adding multilingual navbar support?

Posted: Wed Apr 13, 2016 3:43 pm
by garvinhicking
Nice! Thanks for sharing the code. Even though it's not the most elegant solution due to s9y's restriction, I think it is cool to be able to workaround that :-)