• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
language subdomains

#9
(11-18-2017, 01:42 PM)sneakyimp Wrote: I've been helping to rebuild a largish site in CodeIgniter 3 for some time now. Our plan all along has been to specify the language to be displayed as a subdomain. E.g., spanish will be es.example.com and italian would be http it.example.com etc.  We accomplished this by extending the CI_Controller class and adding a $language class var. In our constructor, we sniff out the value for the current language from the domain:

Code:
 // Set site display language from sub-domain only.
 $this->language = self::get_site_display_language_preference();
where the get_site_display_language_preference() function looks like this:
Code:
/**
* @return string valid language code
*
*/
public static function get_site_display_language_preference()
{
// Extract language code from sub-domain.
$domain_parts = explode(".", $_SERVER['HTTP_HOST']);
$possible_subdomain_language = array_shift($domain_parts);

if (self::is_allowed_subdomain_language($possible_subdomain_language)) {
return $possible_subdomain_language;
} else {
return config_item('language'); // default
}

} // get_site_display_language_preference()

This seems to work pretty well. In any controller, we can happily load the appropriate language file like so:
Code:
$this->lang->load('menus/main_menu', $this->language);
And this command knows to look in the right location for the file.

The problem we have is with redirects. I created these two methods in a test controller:
Code:
/**
* Quick test to see how redirection works with language issues
*/
public function redir() {
redirect("/test/arrival");
}

public function arrival() {
die("this is arrival, lang=" . $this->language);
}
If I visit es.example.com/test/redir in my browser, this redirect drops the language portion of my domain and redirects to example.com/test/arrival. The desired behavior would be for it to preserve the "es" in the original url and redirect to es.example.com/test/arrival. This problem appears to happen because the redirect function makes use of the site_url function which looks like this:
Code:
if ( ! function_exists('site_url'))
{
/**
* Site URL
*
* Create a local URL based on your basepath. Segments can be passed via the
* first parameter either as a string or an array.
*
* @param string $uri
* @param string $protocol
* @return string
*/
function site_url($uri = '', $protocol = NULL)
{
return get_instance()->config->site_url($uri, $protocol);
}
}
This apparently just prepends whatever value you have for base_url in application/config/config.php.

What do you guys recommend in this situation? We can put this type of code in the config.php file but I wonder if it's very safe or whatever.:
Code:
$config['base_url'] = 'https://example.com/';

// base_url needs to switch based on whether there is a language subdomain in use
$domain_parts = explode(".", $_SERVER['HTTP_HOST']);
$subdomain = array_shift($domain_parts);
$allowed_subdomains = array('es', 'it', 'de');
if (in_array($subdomain, $allowed_subdomains)) {
$config['base_url'] = 'https://' . $subdomain . '.example.com/';
}

Is there any recommend best practice for using subdomains?
English only
Reply


Messages In This Thread
language subdomains - by sneakyimp - 11-18-2017, 01:42 PM
RE: language subdomains - by Kaosweaver - 11-20-2017, 06:53 AM
RE: language subdomains - by Narf - 11-20-2017, 07:31 AM
RE: language subdomains - by Kaosweaver - 11-20-2017, 10:56 AM
RE: language subdomains - by sneakyimp - 11-21-2017, 12:17 PM
RE: language subdomains - by sneakyimp - 11-22-2017, 01:17 PM
RE: language subdomains - by sneakyimp - 11-22-2017, 03:39 PM
RE: language subdomains - by Kaosweaver - 11-24-2017, 09:12 AM
RE: language subdomains - by myhanet - 12-07-2017, 05:05 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.