qtranslate-slug icon indicating copy to clipboard operation
qtranslate-slug copied to clipboard

Translate support in Custom links in Nav Menus

Open not-only-code opened this issue 12 years ago • 52 comments

I think andreu is talking about the custom links where you input the links manually. It is not a bug. I assume you just didn't implement the functionality to that feature. However, like andreu said, by manually applying qTranslate's tags (<--:en-->), it will automatically become compatible with the plugin's nav menu translator.

not-only-code avatar Dec 19 '12 01:12 not-only-code

do you have a solution at the moment?

jeroenschermer avatar May 23 '13 13:05 jeroenschermer

You can very easily add different languages by using the shortcodes: [:en]Home [:de]Startseite [:tr]Anasayfa

You just add these at the "Navigation Label" of your menu.

Deelux avatar May 27 '13 14:05 Deelux

thanks, this works great

2013/5/27 Deelux [email protected]

You can very easily add different languages by using the shortcodes: [:en]Home [:de]Startseite [:tr]Anasayfa

— Reply to this email directly or view it on GitHubhttps://github.com/not-only-code/qtranslate-slug/issues/20#issuecomment-18501820 .

http://www.arkvannoach.com/

jeroenschermer avatar May 27 '13 14:05 jeroenschermer

Using a quick tag works for the name of the link, but I'm actually curious about the URL of the custom link. In my case, my custom link is an inline anchor link. WP doesn't seem tike quick tags or xml language tags. It just removes everything.

robertdiaz avatar Aug 16 '13 20:08 robertdiaz

Will it be possible to define a different link for each language too, i would really need this. So somthing like "[:de]http://google.de[:en]http://google.com" could work.

renesprringmann avatar Aug 29 '13 09:08 renesprringmann

After all day of reaseach on custom link I was able to fix it by creating two menus, one in english (Nav_en) and one in spanish (Nav), which is also selected as default menu, and default language is Spanish. Then on Header.php I added this code:

Let me know if it worked for you, Cheers!!

jsapoznik avatar Oct 07 '13 22:10 jsapoznik

Wow, this worked for me as well.

If you only have 1 or 2 custom links in your navigation, you could just add some inline css to your header with your if statement solution, in order to show/hide the custom links depending on the language.

For example:

robertdiaz avatar Oct 13 '13 03:10 robertdiaz

Added as enhancement https://github.com/not-only-code/qtranslate-slug/issues/55

not-only-code avatar Oct 13 '13 11:10 not-only-code

So I just encounter a problem with the code... It doesn't work with Chrome installed in languages that are neither english or spanish... Maybe it's missing one generic line of code for a default language, Any thoughts on this?

jsapoznik avatar Oct 24 '13 20:10 jsapoznik

For example, to use english as the fallback language, use this:

} else {
// the default lang, u can use the english code
}

On Thursday, October 24, 2013, Johanna wrote:

So I just encounter a problem with the code... It doesn't work with Chrome installed in languages that are neither english or spanish... Maybe it's missing one generic line of code for a default language, Any thoughts on this?

— Reply to this email directly or view it on GitHubhttps://github.com/not-only-code/qtranslate-slug/issues/20#issuecomment-27027727 .

LC43 avatar Oct 24 '13 20:10 LC43

So for example

if (qtrans_getLanguage()=='es') {
  wp_nav_menu( array('menu' => 'Nav' ));
} else {
  wp_nav_menu( array('menu' => 'Nav_en' ));
}

LC43 avatar Oct 24 '13 20:10 LC43

Try and copy/paste this in your function.php:

// Makes menu urls work with Qtranslate
function qtranslate_menu_item( $menu_item ) {
  if (stripos($menu_item->url, get_site_url()) !== false){
    $menu_item->url = qtrans_convertURL($menu_item->url);
  }     
  return $menu_item;
}

add_filter('wp_setup_nav_menu_item', 'qtranslate_menu_item', 0);
/**************************************************/

bmachuel avatar Apr 06 '14 07:04 bmachuel

This last answer works like a charm but not for themes with a top left icon going to the home page. I use Responsive theme, and there is an icon the upper left corner of the header, above the main menu, which gets the user to the home page. This link resets the current language.

dvkch avatar Apr 07 '14 21:04 dvkch

Woops, actually the link is alright. Sorry for the previous post

dvkch avatar Apr 07 '14 21:04 dvkch

Thanks bmachuel! It works!

oaigudmundsson avatar Apr 08 '14 12:04 oaigudmundsson

hello,

I tried the "bmachuel" solution, and looks solve the problem of showing the url in the menu.

My problem is when I try to save it. When I put the url with tags: "http://xxxxx/pt

http://xxxxx/en http://xxxxx/es"

The string is not commited in the database when i save in Admin page.

I have to updade in database the string, so I can retrieve the url with the bmachuel solution.

Somebody have any idea?

jlnery-zz avatar Apr 11 '14 20:04 jlnery-zz

Thank you, bmachuel, works perfectly. I just have one little issue: I have a language selector in one menu (custom links which link to the domain itself (language 1) and the domain/sl (language 2). Your function now translates these links as well, turning them into domain/sl (language 1) and domain/sl/sl (language 2).

Is there away to make your function work only on one of the two menus? Or not allow it to translate in the second?

Thanks for your help,

derbub

derbub avatar Apr 18 '14 08:04 derbub

Hi Derbub,

Have you tried Qtranslate's built-in language selector?

Benoît

bmachuel avatar Apr 18 '14 11:04 bmachuel

@bmachuel : thanks for your solution but when I put your code at the end of function.php (WP 3.8.1), an error occurs : Fatal error: Call to undefined function add_filter() in /home/MyName/public_html/wp-includes/functions.php on line 4205

lepatachou avatar Apr 29 '14 20:04 lepatachou

@lepatachou no no no , that code should go into your theme's function.php. don't edit that file wp-includes/functions.php, its the wordpress core file. If you update wp, all the changes will be lost.

LC43 avatar Apr 29 '14 20:04 LC43

I don't understand the posted solution of you bmachuel. I want the shortcodes to work with my custom links. By adding your code to functions.php it still doesnt save the shortcoded custom link fields, so it doesn't do anything.

Please explain further for non-coders.

wouts avatar Apr 30 '14 14:04 wouts

@LC43 : Thanks for your remark. It works fine now. @wouts : shortcodes are not saved in data base but added, by this code, dynamicaly in the link.

lepatachou avatar Apr 30 '14 20:04 lepatachou

Aha, yes it works. I checked the code and it looks for your site url (your domain) in the custom link. It didnt work because i had my links like this: /#link (without the domain in front). made them like this: http://mydomain.com/#link And it works. Thanks for your reply lepatachou, and also big thanks to bmachuel for the code ofcourse!

wouts avatar Apr 30 '14 21:04 wouts

Hey bmachuel,

the widget is not really an option for me, as I need to include the language selector in the top primary menu. Is there maybe a way to disable your function for a certain menu, keeping it working for the other menus?

derbub avatar May 04 '14 13:05 derbub

@derbub Can you post a link to your webpage so that we have an idea of what you are trying to achieve?

bmachuel avatar May 05 '14 07:05 bmachuel

@bmachuel thanks, qtranslate_menu_item is a lifesaver!

sardbaba avatar Jun 24 '14 19:06 sardbaba

hi, i have inserted the @bmachuel code in my theme functions.php, but, a question, how i can insert my custom link in menu? I need to make a menu with links to an external site, but with correct language redirection. qtrans Shortcodes don't works. Can u help please?

jp1976 avatar Jun 25 '14 20:06 jp1976

it works, but i have checked the code and it looks for your site url, now i have another problem, i need to insert an external custom link (from another site) and NOT from my site.

jp1976 avatar Jun 25 '14 23:06 jp1976

@bmachuel hi, just great piece of code. Is it possible to somehow exclude 1 or more items from menu? I mean everywhere give me www.xxxxxx.com/pl/xxxxxx,but i need i one item to give me wtihout language- just pure www.xxxxx.com/xxxx. is it somehow with some class or what? Thanks all best

ratulo avatar Jul 01 '14 11:07 ratulo

Hello,

@wouts - how did you add the custom urls? Because I have pasted the code from @bmachuel and also tried lots of different ways to put the links in the menu (I am ok with the headers using "За тебFor You" for instance) but with the urls I have no success. And the theme doesn't give me to create different menus (because it has different navigations and for each navigation it only gives me one option to put only one menu there).

Please, I could really use some help here :)

pollydesh avatar Mar 31 '15 19:03 pollydesh