<?xml version="1.0"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Forum posts to 'Archive'</title>
		<link>http://www.silverstripe.org/archive/rss</link>
		<atom:link href="http://www.silverstripe.org/archive/rss" rel="self" type="application/rss+xml" />
		<description></description>

		
		<item>
			<title>Re: Hack: Pretty URLs and Language Switcher</title>
			<link>http://www.silverstripe.org/archive/show/163242?start=0#post168632</link>
			<description>&lt;p&gt;Awesome, yeah thats the approach we're tending towards as well - especially when statically caching pages we ran into problems with &quot;lang&quot; as a GET param. Sam has used the new RequestHandlingData functionality in trunk to specify the &quot;code&quot; part as the first chunk of a URL for the ModelAsController rule - no need to hack up main.php.&lt;br /&gt;I think it still requires the definition of every language separately, emailed Sam to shine some light on what he did.&lt;/p&gt;&lt;br&gt;&lt;br&gt;Posted to: Hack: Pretty URLs and Language Switcher &lt;a href=&quot;http://www.silverstripe.org/archive/show/163242?start=0#post168632&quot;&gt;Show Thread&lt;/a&gt; | &lt;a href=&quot;http://www.silverstripe.org/archive/reply/163242?start=0#post168632&quot;&gt;Post Reply&lt;/a&gt;</description>
			<pubDate>Fri, 12 Sep 2008 09:46:41 +1200</pubDate>
			<dc:creator>Ingo</dc:creator>
			<guid>http://www.silverstripe.org/archive/show/163242?start=0#post168632</guid>
		</item>
		
		<item>
			<title>Hack: Pretty URLs and Language Switcher</title>
			<link>http://www.silverstripe.org/archive/show/163242#post163242</link>
			<description>&lt;p&gt;So here's my solution to the problem that so many have: I wanted pretty URLs for a multilanguage site, no ugly ?lang parameters.&lt;/p&gt;&lt;p&gt;Four changes:&lt;/p&gt;&lt;p&gt;&lt;strong&gt;1. Tell SS which languages you are ready to take&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Being a complete noob to SS, I had to hardcode this. In your _config.php add this:&lt;br /&gt;&lt;div class=&quot;codesnippet&quot;&gt;&lt;p&gt;i18n::enable();&lt;br /&gt;global $allowed_i18n;&lt;br /&gt;$allowed_i18n = array('en', 'de', 'fr');&lt;/p&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;2. Have SS understand which language to present&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;I defined that the first URL segment is the language element: &lt;a href=&quot;http://mysite.com/en/page/&quot;&gt;http://mysite.com/en/page/&lt;/a&gt;&lt;br /&gt;/sapphire/main.php strips the URL apart, checks if the first segment is in the definition list above and sets the language.&lt;br /&gt;This has two consequences: You can not use a language called 'db' because this is needed for the admin database&lt;br /&gt;maintenance operations, and you can not have page names that are the same as your language codes.&lt;/p&gt;&lt;p&gt;Add this immediately before the default director rules (near line 122 in SS 2.2.2) in /sapphire/main.php&lt;br /&gt;&lt;div class=&quot;codesnippet&quot;&gt;&lt;p&gt;if(substr($url,0,strlen($baseURL)) == $baseURL) $url = substr($url,strlen($baseURL));&lt;/p&gt;&lt;p&gt;global $allowed_i18n;&lt;br /&gt;if(sizeof($allowed_i18n) &amp;gt; 0) {&lt;br /&gt;  $tmp_url = preg_replace( array( '/\/+/','/^\//', '/\/$/'),array('/','',''),$url);&lt;br /&gt;  $tmp_urlParts = split('/+', $tmp_url);&lt;br /&gt;  $lng = array_shift($tmp_urlParts);&lt;br /&gt;  $bSetLang = false;&lt;br /&gt;  if(in_array(strtolower($lng), $allowed_i18n)) {&lt;br /&gt;    $url = &quot;/&quot;.join('/', $tmp_urlParts);&lt;br /&gt;    $_REQUEST['url']  = $url;&lt;br /&gt;    $bSetLang = true;&lt;br /&gt;  }&lt;br /&gt;  if(isset($_GET['lang']) &amp;amp;&amp;amp; in_array(strtolower($_GET['lang']), $allowed_i18n)) {&lt;br /&gt;    $lng = $_GET['lang'];&lt;br /&gt;    $bSetLang = true;&lt;br /&gt;  }&lt;br /&gt;  if($bSetLang) {&lt;br /&gt;    $_GET['lang'] = $lng;&lt;br /&gt;    $_REQUEST['lang'] = $lng;&lt;br /&gt;  }&lt;br /&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;3. Prepare language switcher&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Simply add the following function to your page controller&lt;br /&gt;&lt;div class=&quot;codesnippet&quot;&gt;&lt;p&gt;function LanguageSwitcher() {&lt;br /&gt;        $langs = i18n::get_existing_content_languages();&lt;br /&gt;        $data = new DataObjectSet();&lt;br /&gt;        foreach(array_keys($langs) as $code) {&lt;br /&gt;                if($code == Translatable::current_lang()) {&lt;br /&gt;                        continue;&lt;br /&gt;                }&lt;br /&gt;                $page = Translatable::get_one_by_lang(&quot;SiteTree&quot;, $code, &quot;`SiteTree`.ID = &quot; . Controller::curr()-&amp;gt;ID);&lt;br /&gt;                $data-&amp;gt;push(new ArrayData(array('name' =&amp;gt; i18n::get_language_name($code, true),&lt;br /&gt;                                                'link' =&amp;gt; Director::protocolAndHost() . Director::baseURL() . &quot;$code/&quot; . $page-&amp;gt;URLSegment,&lt;br /&gt;                                                'code' =&amp;gt; $code)));&lt;br /&gt;        }&lt;br /&gt;        return $data;&lt;br /&gt;    }&lt;/p&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;4. Integrate the language switcher into your template&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;I use the following snippet in an include file&lt;br /&gt;&lt;div class=&quot;codesnippet&quot;&gt;&lt;p&gt;&amp;lt;ul class=&quot;LanguageChooser&quot;&amp;gt;&lt;br /&gt;    &amp;lt;% _t('VIEWIN', 'View this site in:') %&amp;gt;&lt;br /&gt;    &amp;lt;% control LanguageSwitcher %&amp;gt;&lt;br /&gt;        &amp;lt;li&amp;gt;&amp;lt;a href=&quot;$link&quot;&amp;gt;$name&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;    &amp;lt;% end_control %&amp;gt;&lt;br /&gt;&amp;lt;/ul&amp;gt;&lt;/p&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;As you will notice I have &quot;borrowed&quot; from others. Standing on the shoulders of giants ...&lt;/p&gt;&lt;p&gt;Please comment and improve.&lt;/p&gt;&lt;p&gt;Cheers.&lt;/p&gt;&lt;p&gt;[edit: changed the main.php hack - setting $_GET['lang'] as well]&lt;br /&gt;[ANOTHER edit: changed the main.php hack - doesn't reset the admin language anymore. that was bad for editing translations]&lt;/p&gt;&lt;br&gt;&lt;br&gt;Posted to: Hack: Pretty URLs and Language Switcher &lt;a href=&quot;http://www.silverstripe.org/archive/show/163242#post163242&quot;&gt;Show Thread&lt;/a&gt; | &lt;a href=&quot;http://www.silverstripe.org/archive/show/163242#post163242&quot;&gt;Post Reply&lt;/a&gt;</description>
			<pubDate>Mon, 08 Sep 2008 07:02:39 +1200</pubDate>
			<dc:creator>Skipper</dc:creator>
			<guid>http://www.silverstripe.org/archive/show/163242#post163242</guid>
		</item>
		

	</channel>
</rss>