Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6809479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:05:30+00:00 2026-05-26T20:05:30+00:00

I was working on my CakePHP 2.0 and wanted to make a language helper

  • 0

I was working on my CakePHP 2.0 and wanted to make a language helper so I don’t have to pass a few language based things in my views. For this I created a LanguageHelper.

My first task was to include the language in all links.

<?php

    class LanguageHelper extends AppHelper{

        public $helpers = array(
             'Html'
        );

        public function link($title, $url = null, $options = array(), $confirmMessage = false){

            if(!isset($options['lang']) || !$options['lang']){
                  $options['lang'] = Configure::read('Language.default');
            }

            return $this->Html->link($title, $url, $options, $confirmMessage);
        }
    }
?>

Now in my view I use the following:

<?php echo $this->Language->link('Link', array('controller' => 'pages', 'action' => 'home')); ?>

Expected result:

<a href="/nl-be/admin/pages/home">Link</a>

Actual result:

<a lang="nl-be" href="/admin/pages/home">Link</a>

Even using the following:

<?php echo $this->Language->link('Link', array('controller' => 'pages', 'action' => 'home', 'lang' => Configure::read('Language.default'))); ?>

Gives me a wrong result:

<a lang="nl-be" href="/nl-be/admin/pages/home">Link</a>

The following is in my routes:

Router::connect('/', array('controller' => 'app', 'action' => 'defineLanguage'), array('lang' => '[a-zA-Z]{2}[-]{1}[a-zA-Z]{2}'));
Router::connect('/:lang', array('controller' => 'pages', 'action' => 'home'), array('lang' => '[a-zA-Z]{2}[-]{1}[a-zA-Z]{2}'));

$prefixes = Configure::read('Routing.prefixes');

foreach($prefixes as $prefix){
    Router::connect('/:lang/' . $prefix, array('prefix' => $prefix, $prefix => true, 'controller' => 'pages', 'action' => 'index'), array('lang' => '[a-zA-Z]{2}[-]{1}[a-zA-Z]{2}'));
    Router::connect('/:lang/' . $prefix . '/:controller', array('prefix' => $prefix, $prefix => true), array('lang' => '[a-zA-Z]{2}[-]{1}[a-zA-Z]{2}'));
    Router::connect('/:lang/' . $prefix . '/:controller/:action', array('prefix' => $prefix, $prefix => true), array('lang' => '[a-zA-Z]{2}[-]{1}[a-zA-Z]{2}'));
}

Anyone know a way to get the expected result?

EDIT:

I Should also mention that the following does give me the expected result:

<?php echo $this->Html->link('Link', array('controller' => 'pages', 'action' => 'home', 'lang' => Configure::read('Language.default'))); ?>

SOLUTION:

class LanguageHelper extends AppHelper{

public $helpers = array(
    'Html'
);

public function link($title, $url = null, $options = array(), $confirmMessage = false){

    if(!isset($url['lang']) || !$url['lang']){
    $url['lang'] = Configure::read('Language.default');
    }

    return $this->Html->link($title, $url, $options, $confirmMessage);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:05:30+00:00Added an answer on May 26, 2026 at 8:05 pm

    I’m still working on 1.3 but I assume that did not changed: you have to put ‘lang’ parameter into $url array in your helper, not into $options array.

    Last line of your helper should be:

    $this->Html->link($title, array_merge($url, array('lang' => $options['lang']), $options, $confirmMessage);
    

    Additionally you should use other variable to avoid unnecessary attribute in your link.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently started working with CakePHP but there are a few things I don't
let's say i have: http://some-domain/application/controller/action/parameter This is somehow working in cakePHP. Now I want
I'm working on a French language site built in CakePHP. I have tried multiple
Using CakePHP 1.3 , I have a (working) form that has dynamically created form
I am working on CakePHP, and I have my app working in my localhost.
I have a RESTful API set up and working with CakePHP using mapResources() and
I'm new to cakephp so have just been working through the cookbook and am
I am new to cakephp and have been working through the Apress book Beginning
I have installed CakePHP in my test server and it is working fine. When
I'm trying to convert this CakePHP tutorial I'm working on to the fat model

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.