2017 © Pedro Peláez
 

yii2-extension yii2-hostname-language

Simple component which helps to configure the app locale based on the hostname

image

leinonen/yii2-hostname-language

Simple component which helps to configure the app locale based on the hostname

  • Thursday, January 14, 2016
  • by lordthorzonus
  • Repository
  • 2 Watchers
  • 0 Stars
  • 247 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Yii2 Hostname Language Component

Build Status SensioLabsInsight Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

This simple component sets the Yii's app locale based on the requests hostname. So for example, if you have my-awesome-app.com which is English version of your site and my-awesome-app.fi which is Finnish and they both point to the same Yii2 app, this component sets the app language automatically on every request., (*2)

Installation

Require this package, with Composer, in the root directory of your project., (*3)

composer require leinonen/yii2-hostname-language

Configuration

The component is configured in the main config. The pattern for configuring the hosts is 'hostname' => 'language'. The scheme of the request (http/https) must be left out., (*4)

use leinonen\HostnameLanguage\LanguageSelector;

'bootstrap' => ['hostLanguage'],
'components' => [
    'hostLanguage' => [
        'class' => LanguageSelector::class,
        'hosts' => [
             'example.com' => 'fi-FI',
             'foobar.se' => 'se-SE',
      ]
    ]
],

URL Manager

You can also configure a custom URL manager which allows creating urls to another language version of your site. This extends the \yii\web\UrlManager so all the normal features are also available., (*5)

use leinonen\HostnameLanguage\UrlManager;

'components' => [
    'urlManager' => [ 
      'class' => UrlManager::class,
      'enablePrettyUrl' => true,
      'showScriptName' => false,
    ]
]   

It then works by feeding a language parameter to the URL helper function:, (*6)

Url::to(['site/my-route', 'language' => 'en-US'], true);

Remember to set scheme param to true, cause you need to create absolute urls. If you are already using an url parameter called language it can easily be changed with the config:, (*7)

'urlManager' => [ 
    ...
    'languageParam' => 'lang'
]

And then:, (*8)

Url::to(['site/my-route', 'lang' => 'en-US'], true);

The Versions

14/01 2016

dev-master

9999999-dev https://github.com/lordthorzonus/yii2-hostname-language

Simple component which helps to configure the app locale based on the hostname

  Sources   Download

MIT

The Requires

 

The Development Requires

by Juuso Leinonen

language yii2 locale i18n hostname

27/10 2015

0.5.0

0.5.0.0 https://github.com/lordthorzonus/yii2-hostname-language

Simple component which helps to configure the app locale based on the hostname

  Sources   Download

MIT

The Requires

 

The Development Requires

by Juuso Leinonen

language yii2 locale i18n hostname