Jump to content

Simple internal multilingual link


milo695
 Share

Recommended Posts

Hi all,
I need help creating link in CK editor that would target my translated page, so it depends on what language user has selected.

something like:

<a href="?id=1450">view all</a>

would target:

<a href="en/onion/">Onion</a>
<a href="de/zwiebel"></a>

depending on user defined language.
Thank you

Edited by milo695
Tags added
Link to comment
Share on other sites

Just to make sure I understand this correctly: you're looking for a way to insert a link to a page in a certain language, no matter what the current language is when the link is being clicked?

Link to comment
Share on other sites

1 hour ago, BitPoet said:

Just to make sure I understand this correctly: you're looking for a way to insert a link to a page in a certain language, no matter what the current language is when the link is being clicked?

Hey, it's something like this in a multilingual website:
I insert standard link via CKeditor to a internal page: <a href="/booking/">Book Now</a>
Now, I need that link to go to translated page depending of user language preference. If user changed language to italian from the menu, then the link will go to:
/it/booking/ if german than /de/booking/ etc...

I'm trying to make a COVID-19 announcement on my main page, but not via main menu template file but from editor.

But if that is not possible I can place the link somewhere in the template file in order to work, but I need code example in order to do that.
Thanks BitPoet

 

Edited by milo695
Link to comment
Share on other sites

Here's a somewhat versatile, albeit not overly beautiful to look at (in the backend) approach using Hanna Code. It's not really wysiwyg in the backend, but it's easier to maintain than hardcoding stuff in the template.

A short step-by-step instructions (looks more complicated than it really is):

Download and install Textformatter Hanna Code module
Download and install Hanna Code Dialog module

Go to "Setup" -> "Hanna Code" and create a new Hanna code, let's name it "languagelink", with type "PHP" and the two arguments "targetlink" and "linktext".

hanna1.thumb.png.06f529ee359d88f8c35fb61f23f19cb2.png

In the "Code" tab, add the following PHP code:

<?php

if(! empty($targetpage))
    $tgt = $pages->get($sanitizer->pagePathName($targetpage));
else
    $tgt = $page;

$url = $tgt->localUrl($user->language);

echo "<a href='{$url}'>{$linktext}</a>";

 

Go into the configuration of your CKEditor field. On the "Details" tab, add the Hanna Code Textformatter, which will then be applied to the field content each time it is rendered:

hanna2.png.c5820408d58f25514209b7afb9f6712f.png

 

On the Input tab, go to the CKEditor Toolbar settings and add HannaDropdown on its own line, which will add the menu entry for CKEditor:

hanna3.png.e254c7c42feeca7421cf5e1b854b70e6.png

Now you have everything set up and can start inserting your languagelinks.

 

Edit the page in question. You'll now find the menu entry "Insert Hanna tag". Click that and select "languagelink".

hanna4.thumb.png.66df0473cdd89d0a855706315b76d3f6.png

 

Enter the path to the target page and the text for the link there.

hanna5.thumb.png.fc7c2d7c658b98f8f9e49003c079153a.png

 

Your Hanna Code is now in the CKEditor field.

hanna6.thumb.png.33f90d8db59568dc389b82e90253b9e3.png

 

Save the page and view it in the frontend.

Page viewed in the default language:
hanna7default.png.f9660af401e44a6f07f188c3a2dee525.png

 

Page viewed in language "en":

hanna7en.png.c4fad29f4208e9a49275e6079b22adb5.png

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...