Last active
November 25, 2015 13:03
-
-
Save webspilka/9f96ef301c99c93eac24 to your computer and use it in GitHub Desktop.
Silverstripe fluent locale switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Отримати сслику на головну сторінку сайту | |
// для локалі по замовчанню отримуєм / , для всіх інших /locale-alias/ | |
public function getBaseURL($locale = null){ | |
if(empty($locale)) $locale = Fluent::current_locale(); | |
// Build domain-specific base url | |
$base = Director::baseURL(); | |
if($locale === Fluent::default_locale()) return $base; | |
// Append locale otherwise | |
return Controller::join_links( | |
$base, | |
Fluent::alias($locale), | |
'/' | |
); | |
} | |
// щоб не прописуваты $BaseHref<% if $CurrentLocale==en_US %>en/<% else_if $CurrentLocale==uk_UA %>uk/<% end_if %> | |
// причому для локалі по замовчанню верне $BaseHref/ без аліаса локалі | |
// реалізацію взяв з Fluent::locale_baseurl, але там для локалі позамовчанню не видавався лінк на локаль | |
public function getLocaleBaseURL($locale = null){ | |
//Debug::show($this); | |
if(empty($locale)) $locale = Fluent::current_locale(); | |
// Build domain-specific base url | |
$base = Director::baseURL(); | |
// Don't append locale to home page for default locale | |
//if($locale === self::default_locale()) return $base; | |
//if($this->URLSegment === 'home') return $base; | |
// Append locale otherwise | |
return Controller::join_links( | |
$base, | |
Fluent::alias($locale), | |
'/' | |
); | |
//return $baseURL; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{$getLocaleBaseURL} - завжди ссилається на поточну локалізацію сайту /{current_locale}/ | |
{$getBaseURL} - майже те саме, тільки якщо на головній сторінці в локалі по замовчанню просто видає / а не /{current_locale}/ | |
Приклади | |
Метод {$getBaseURL} дозволяє замінити шаблон фомування ссилки на головну сторінку сайту | |
<a class="navbar-brand" href="$BaseHref<% if $CurrentLocale==en_US %>en/<% else_if $CurrentLocale==uk_UA %>ua/<% end_if %>" rel="home" title="$SiteConfig.Title">{$SiteConfig.Title}</a> | |
на | |
<a class="navbar-brand" href="{$getBaseURL}" rel="home" title="$SiteConfig.Title">{$SiteConfig.Title}</a> | |
якщо в коді шаблону є статичні сслики типу /ru/contact/ -> {$getLocaleBaseURL}contact/ | |
<%-- показати меню вибору локалізацій --%> | |
<%-- include ShowLocaleMenu --%> | |
<% if Locales %> | |
<ul class="nav nav-lang"> | |
<% loop Locales %> | |
<li class="<% if $LinkingMode==current %>active<% else %>$LinkingMode<% end_if %>"> | |
<a href="$Link.ATT" <% if $LinkingMode != 'invalid' %>rel="alternate" hreflang="$LocaleRFC1766"<% end_if %>>$Alias</a> | |
</li> | |
<% end_loop %> | |
</ul> | |
<% end_if %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment