Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
URI class extended to support many languages
  • I've extended the URI class so it can support this kind of routes in a transparent way
    http://yoursite.com/en/blog/entry.html
    http://yoursite.com/es/blog/entry.html To implement it, create the file /fuel/app/classes/uri.php class Uri extends Fuel\Core\Uri { public function __construct($uri = NULL)
    {
    if ($uri === NULL)
    {
    $uri = static::detect();
    }
    $this->uri = trim($uri, '/');
    $this->segments = explode('/', $this->uri);
    $this->detect_language();
    } public function detect_language()
    {
    if(!count($this->segments)) return false;
    $first = $this->segments[0];
    $languages = Config::get('languages');
    if(in_array($first,$languages))
    {
    array_shift($this->segments);
    $this->uri = implode('/',$this->segments);
    Config::set('language',$first);
    }
    }
    }
    In the config file add this $key => $value pair:
    'languages' => array (
    'en','es','fr'
    ), where the values are the languages you want to support in your application. Hope this help.
    Best Regards. Santiago.
  • C.K.Y your code works well with v 1.2.. thanks!
  • I had a look it helped me, th least thing is my link 'fr/histoire' is working ,but on my page 'histoire' all the path (img,css...) are false example :'fr/assets/css/common.css'. do you know hot to fix that?
  • http://docs.fuelphp.com/classes/asset.html CSS:
    <?php echo Asset::css('common.css'); ?>
    //or
    <?php echo Asset::css(array('common.css', 'common2.css')); ?>
    

    [removed]
    <?php echo Asset::js('jquery.js'); ?>
    //or
    <?php echo Asset::js(array('jquery.js', 'common.js')); ?>
    

    etc.
  • thanks a lot you really helped me to do what I wanted!
  • You're Welcome ;)
  • Santiago, I'm using your bit of code and it works, brilliant, thanks a lot!
  • This code is no longer working with the latest versions of Fuel,
    is there an alternative, how can I make this work again? (Uri class changed considerably, I think) Thank you!
  • This is the basic version of what I posted four months ago... http://fuelphp.com/forums/topics/view/253
  • i'm searching for it too
  • There is my code, it's based on this topic but with small modifications.
    Tested on v1.1. app/classes/uri.php
    class Uri extends Fuel\Core\Uri
    {
        public function __construct($uri = NULL)
        {
            parent::__construct($uri);
            $this->detect_language();
        }
    
        public function detect_language()
        {
            if ( ! count($this->segments))
            {
                return false;
            }
    
            $first = $this->segments[0];
            $locales = Config::get('locales');
    
            if(array_key_exists($first, $locales))
            {
                array_shift($this->segments);
                $this->uri = implode('/', $this->segments);
    
                Config::set('language', $first);
                Config::set('locale', $locales[$first]);
            }
        }
    }
    

    app/config/config.php
       /**
         * Localization & internationalization settings
         */
        'language'           => 'en', // Default language
        'language_fallback'  => 'en', // Fallback language when file isn't available for default language
        'locale'             => 'en_US', // PHP set_locale() setting, null to not set
        'locales'            => array(
            'en' => 'en_US',
            'ro' => 'ro_MD',
            'ru' => 'ru_RU',
        ),
    

    app/bootstrap.php
    Autoloader::add_classes(array(
        // Add classes you want to override here
        // Example: 'View' => APPPATH.'classes/view.php',
        'Uri' => APPPATH.'classes/uri.php',
    ));
    
  • I'm using your uri class I'm a beginner, and I'm looking how to do link to use this class, If I have link like 'fr/histoire' fuel think that my controller is 'fr', how do you do your links with this class
    thank you
  • Actually I use my custom function for link creation. Something like this:
    function url($uri = '', $named_params = array())
    {
        $lang = Config::get('language');
        if ( ! empty($uri))
        {
            $lang .= '/';
        }
    
        if ($named_uri = Router::get($uri, $named_params))
        {
            $uri = $named_uri;
        }
    
        return Uri::create($lang.$uri);
    }
    

    HTML:
    <a href="<?php echo url('histoire'); ?>">Link Name</a>
    
  • thank you it works perfectly to create links, but I still have the same problem, fuel think that 'fr' is my controller so my link goes to en error page, my controller is histoire, so do you know how I can reach it? my routes.php is like
    return array(
    '_root_' => 'welcome/index', // The default route
    '_404_' => 'welcome/404', // The main 404 route 'fr' => 'welcome/index',
    'it' => 'welcome/index', 'fr/histoire' => 'welcome/histoire',
    'it/storia' => 'welcome/histoire',
    ); and where do you put the your code so each page can reach it, in the uri class it cause an error for the 'welcome controller' page? thank you
  • They does't work because language segment is removed from URI Segments pull after language detection. You can use something like this: routes.php
    return array(
        '_root_' => 'welcome/index', // The default route
        '_404_' => 'welcome/404', // The main 404 route
    
       '(histoire|storia)' => 'welcome/histoire',
    );
    

    Request like: http://my.domain/{lang}/histoire
    http://my.domain/{lang}/storia will be load: welcome/histoire. My opinion is to make it simple: routes.php
    return array(
        '_root_' => 'welcome/index', // The default route
        '_404_' => 'welcome/404', // The main 404 route
    
       'history' => 'welcome/history',
    );
    

    and requests:
    http://my.domain/fr/history
    http://my.domain/it/history
  • when I change my routes.php to return array(
    '_root_' => 'welcome/index', // The default route
    '_404_' => 'welcome/404', // The main 404 route 'history' => 'welcome/history',
    );
    my link in my template.php is"fr/http://localhost/fuelphp/intranet/public/histoire' instead of 'fr/histoire' do you know why? where do you put you function url on the welcome controller (histoire is a method of my welcome controller)? thanks for your help
  • https://gist.github.com/1844219 I hope this help you to setup language support for your application ;)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion