Reputation: 1914
The SilverStripe Fluent module has a ready-made template to display a simple language switching menu on the front-end.
<% loop Locales %>
<li class="$LinkingMode">
<a href="$Link.ATT" hreflang="$LocaleRFC1766"<% end_if %>>
$Title.XML
</a>
</li>
<% end_loop %>
When it loops "Locales" what is it technically looping? There is no Database table called "Locales".
My goal is to eventually find the variable (or write the function) that returns the language abbreviation (not the country!). So I need something that returns for example nl
rather than nl-NL
(as $LocaleRFC1766
returns).
Upvotes: 5
Views: 741
Reputation: 15794
Locales
is a function in the FluentExtension
extension:
/**
* Templatable list of all locales
*
* @return ArrayList
*/
public function Locales()
{
$data = array();
foreach (Fluent::locales() as $locale) {
$data[] = $this->owner->LocaleInformation($locale);
}
return new ArrayList($data);
}
This is the data that is returned by the LocaleInformation
function:
$data = array(
'Locale' => $locale,
'LocaleRFC1766' => i18n::convert_rfc1766($locale),
'Alias' => Fluent::alias($locale),
'Title' => i18n::get_locale_name($locale),
'LanguageNative' => Fluent::locale_native_name($locale),
'Language' => i18n::get_lang_from_locale($locale),
'Link' => $link,
'AbsoluteLink' => $link ? Director::absoluteURL($link) : null,
'LinkingMode' => $linkingMode
);
Language
was recently added to allow for retrieving just the language abbreviation.
To make use of this we can create a custom LocaleMenu.ss
template that uses the $Language
variable:
<% if $Locales %>
<div class="left">Locale <span class="arrow">→</span>
<nav class="primary">
<ul>
<% loop $Locales %>
<li class="$LinkingMode">
<a href="$Link.ATT" <% if $LinkingMode != 'invalid' %>rel="alternate" hreflang="$Language"<% end_if %>>$Title.XML</a>
</li>
<% end_loop %>
</ul>
</nav>
</div>
<% end_if %>
Upvotes: 6