Jump to content

Multi language - template


nikola
 Share

Recommended Posts

I'm doing a multilanguage site that will contain 3 languages with multi tree approach.

I'll have tree set up as following:

Home

- hr

  - page 1

  - page 2

  - ...

- en

  - page 1

  - page 2

  - ...

- de

  - page 1

  - page 2

  - ...

Pages under each language branch are using language specific templates.

I want to avoid having too many templates all together, so my question is how to accomplish this scenario:

Instead using language specific templates, I want to use let's say, one template that will have language specific code based on hr, en or de selection.

How can I check if page is under the "hr" branch and then use specific code for "hr"? Same procedure will be for "en" and "de", all contained in one template.

Link to comment
Share on other sites

That's an easy scenario. Code of VKNT is ok but not working for all pages and levels. Since it uses only $page->parent.

If we assume the language tree branches are under the root level like /hr/, /en/ you can check for it in the url using strpos or alike, or by  using $page->rootParent->name == 'hr' for example. Your choice.

I got a language check that is like this, saving it in a session is not required but I needed it in this project. Setting a $lang and a $langpath variable is also project specific. But you get the idea.

<?php

// get path of request for finding langpath
$url = $page->path;

if($session->lang){
// if on langpath as in session or requested by /objects/...
if(strpos($url,$session->langpath) !== FALSE ){
	$lang = $session->lang;
	$langpath = $session->langpath;
}else{
	$lang = '';
}
}
else{
$lang = '';
}

if($lang == ''){
if(strpos($url,"/de-ch/") !== FALSE){
	$langpath = 'de-ch';
	$lang = 'de';
}
else if(strpos($url,"/en-ch/") !== FALSE){
	$langpath = "en-ch";
	$lang = 'en';
}
else if(strpos($url,"/fr-fr/") !== FALSE){
	$langpath = "fr-fr";
	$lang = "fr";
}
else if(strpos($url,"/en-fr/") !== FALSE){
	$langpath = "en-fr";
	$lang = "en";
}
else if(strpos($url,"/de-de/") !== FALSE){
	$langpath = "de-de";
	$lang = "de";
}
else{
	$langpath = "de-ch";
	$lang = "de";	
}

$session->set('lang',$lang); 
$session->set('langpath',$langpath);

}

/**
*  YAML Language files
*	import yaml language file for common translations
*  
*  key: "value"
*
*  Available in php with $txt['key']
*/

require_once($config->paths->root . "site/libs/yaml/sfYaml.php");
require_once($config->paths->root . "site/libs/yaml/sfYamlParser.php");

// instance a parser object
$yaml = new sfYamlParser();

// parse language file
$txt = $yaml->parse(file_get_contents($config->paths->root . "site/languages/" . $lang . ".yaml"));

?>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...