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 8462789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:16:06+00:00 2026-06-10T14:16:06+00:00

I would like to add a language code segment to my URIs in CodeIgniter,

  • 0

I would like to add a language code segment to my URIs in CodeIgniter, but I’d like to somehow — possibly with routing — force the browser to stop interpreting a language code as a path.

I haven’t yet successfully implemented any of the i18n libraries in my CodeIgniter install, and I’m fairly certain my problem is simple enough to solve without a library anyway.

The method I had in mind was simply to load the appropriate language files respective of the language code that appears in the URI.

For example

http://example.com/about        // Default language
http://example.com/sv/about     // Load Swedish language

Here’s the controller logic:

<?php
//  ** Update **
//  The following updated code works, as long as the language code appears
//  at the end of the URI, e.g, http://example.com/about/sv
//
//  Ideally, I would like the language code segment to always appear first.
//
//  There is also the problem of keeping the language selected while
//  navigating around the site…

class Pages extends CI_Controller {
    public function view ($page = 'home') {

        if (!file_exists('application/views/pages/'.$page.'.php'))
            show_404();

        $uri = explode("/", $_SERVER['REQUEST_URI']);
        $lang_code = end($uri);

        $data['title'] = $lang_code;

        switch ($lang_code) {
            case "sv": $the_language = "swedish"; break;
            case "no": $the_language = "norwegian"; break;
            default: $the_language = "english";
        }

        $this->lang->load('general',$the_language);
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
    }
}

?>

Am I going about this the wrong way? If so, why? Please avoid canned responses.

  • 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-06-10T14:16:07+00:00Added an answer on June 10, 2026 at 2:16 pm

    I would go with CI routing:

    $route['([a-z]+)/([a-z]+)'] = "pages/view/$2";
    

    Then, your controller would look something like this:

    <?php if (! defined('BASEPATH')) exit('No direct script access');
    
    class Pages extends CI_Controller {
    
    function __construct() {
        parent::__construct();
        $this->language = $this->uri->segment(1);
    }
    
    function view($page = 'home')
    {
        /* other stuff */
        $this->lang->load('general',$this->language);
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
    }
    

    }

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

Sidebar

Related Questions

I'd like to add a keyword to my language. This keyword would only have
I would like to add a small dice-rolling effect to my Javascript code. I
I would like to add a small code snip to check out whether an
I have own project and i would like add for this class same as
I have a ListView and each item have a TextView. I would like add
I would like to add a GestureDetector to all views (view groups) of an
I would like to add a custom calculation method to a managed object (which
I would like to add roots to a VirtualTreeView http://www.delphi-gems.com/index.php/controls/virtual-treeview with a thread like
I would like to add a hash into an array using Ruby version 1.8.7:
I would like to add up the number of line changes in an SVN

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.