Jump to content

Simple internal multilingual link


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

Posted (edited)
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".


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


if(! empty($targetpage))
    $tgt = $pages->get($sanitizer->pagePathName($targetpage));
    $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:



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:


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".



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



Your Hanna Code is now in the CKEditor field.



Save the page and view it in the frontend.

Page viewed in the default language:


Page viewed in language "en":


  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By Peter Knight
      I have a list of pages all based on the same template but I want to exclude 2 by page ID
      $expertises = $pages->find("template=case-type, sort=sort"); foreach ($expertises as $expertise); I know I could change their template or add an exclude checkbox to the template etc
      Wondering if there is a page=not(4989,9877) type of thing in the API to exclude pages by ID?
      I can't find anything in the API docs but think I may be searching for the wrong phrase.
    • By milo695
      I've added a class in ProcessPageEditLink but when I call it from Editor (attributes > class) nothing happens.
      Also, I've created the class in my custom.css, is there any other css file this class needs to be in?
    • By SwimToWin
      I love nice urls - but I also recognize that they are not always needed nor desired due to the additional steps in page creation.
      Suggested solution:
      Page ID Page ID is a per-template auto-incrementing number. No. series might be added on a per-template basis (to be configured by webmaster). Page url Replace the full page url with an ID (as an optional configuration). Page url might then change to something like: www.example.com/page/1234 Editing (Page Tree is much less important in this scenario.) Like Drupal's Node IDs (nid).
      Why this change? Name may reveal confidential info. Name may not matter on certain templates. Page creation is no longer a two-step process; much like adding a row in MySQL.
      Benefit: This change will make it easier to use ProcessWire for other purposes than websites ("Oh - so ProcessWire is also a finance system!").
    • By virtualgadjo
      Hi all,
      i'm running into a funny  little issue
      i'm working on a multilingual website, default is English, second language French
      in this website i've a form for visitors to submit an event, title, date and so on, when they submit the event, it's stored into a personnal db table
      i've created a module in the admin where admins (well, of course 🙂 ) can't validate or delete the submitted events, thanks to pw, it works like breeze except for one little thing 🙂
      when an admin validate an event, i use the pw api to create a page and fill all the new page fields, among which
      $p = new Page(); $p->of(false); .... $p->title = $titre; $titre being the title of the event submitted and retreived from the database
      the thing is that depending of the language of the admin it fills the title in English/the default language or in French if the admin language is French and what i'm looking for is to always fill the default language title whatever the admin language is, guess, to take advantage of the auto filling of other languages when the default one is filled
      Funny enough i've solved the opposite problem with a multilingual select form field, using
      $filters = $fieldtypes->get('FieldtypeOptions')->getOptions('filtres'); foreach($filters as $f): ... echo $f->title; // it always echo the default language option title // using this echo $liso == 'fr' ? $f->title1019 : $f->title; // i've got the right select options titles displayed in the select // of course, $liso is defined in the _init.php $langid = $user->language->id; $langiso = array(1017 => 'en', 1019 => 'fr'); $liso = $langiso[$langid]; in case anybody has an idea about this page creation 🙂
      i keep searching
      have a nice day
    • By virtualgadjo
      Hi all,
      first of all, as it's my first post here, thanks so much for this incredible CMS, thanks for all my question i've found answers here without even asking them and, of course, i can't say thanks enough to Ryan incredible job...
      i'm currently working on a bilingual website (pw 3.0.165 php 7.2), funny enough (i'm french), this time default language is english and the second one french but i think that doesn't matter...
      this website stores events and i have an page having itself all the events as hidden chidren pages, template for an event is... un_event with many fields, some of them bilingual
      as i'm supposed to display the number of events available on all the pages at different places ("see the xx events" with a link to the event page) in my _init.php file i've set a count of the one_event pages and this is where the funny thing is
      $nevents = $pages->count('template=un_event, include=hidden'); // in the default language see the <?php echo $nevents; ?> language works fine // in french $nevents is 0 $nevents = $pages->count('template=un_event, include=all'); // works fine in both languages but implies that even unpublished pages would be taken in account just to be complete, same behaviour with count($pages->get(1032)->children) count($pages->find('template=un_event, include=hidden')) and so on, in english, correct number as the result, in french 0
      as a test, wondering if it was the fact that this was in the _init.php file, same thing if i use the same code on top of a page (all my files have the namespace processwire statement on top, i'm essentially a php guy 🙂)
      other test, i've done the same thing but setting all the one_event pages as visible (not hidden), same result
      is there something i haven't undestood or have forgotten with this all/hidden option when it comes to multilingual websites? probably 🙂
      have a nice day
  • Create New...