Skip to content
This repository has been archived by the owner on May 30, 2019. It is now read-only.

Commit

Permalink
rename languages twig function to elcodi_languages global
Browse files Browse the repository at this point in the history
  • Loading branch information
xphere committed Feb 24, 2015
1 parent 826f69f commit 3d93272
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/Elcodi/Bundle/LanguageBundle/Resources/config/twig.yml
Expand Up @@ -6,7 +6,7 @@ services:
elcodi.twig_extension.language:
class: %elcodi.twig_extension.language.class%
arguments:
language_manager: @elcodi.manager.language
master_language: @elcodi.locale_iso
- @elcodi.manager.language
- @elcodi.locale_iso
tags:
- { name: twig.extension }
46 changes: 27 additions & 19 deletions src/Elcodi/Component/Language/Twig/LanguageExtension.php
Expand Up @@ -18,7 +18,6 @@
namespace Elcodi\Component\Language\Twig;

use Twig_Extension;
use Twig_SimpleFunction;

use Elcodi\Component\Language\Entity\Interfaces\LanguageInterface;
use Elcodi\Component\Language\Services\LanguageManager;
Expand Down Expand Up @@ -57,14 +56,14 @@ public function __construct(
}

/**
* Return all filters
* Returns a list of global variables to add to the existing list.
*
* @return Twig_SimpleFunction[] Filters created
* @return array An array of global variables
*/
public function getFunctions()
public function getGlobals()
{
return array(
new Twig_SimpleFunction('languages', array($this, 'getLanguages')),
'elcodi_languages' => $this->getLanguages(),
);
}

Expand All @@ -77,21 +76,10 @@ public function getLanguages()
{
$languages = $this
->languageManager
->getLanguages();
->getLanguages()
->toArray();

$masterLanguage = $languages
->filter(function (LanguageInterface $language) {
return $language->getIso() === $this->masterLocale;
})
->first();

$languages->removeElement($masterLanguage);
$otherLanguagesArray = $languages->toArray();

return array_merge(
[$masterLanguage],
$otherLanguagesArray
);
return $this->promoteMasterLanguage($languages);
}

/**
Expand All @@ -103,4 +91,24 @@ public function getName()
{
return 'language_extension';
}

/**
* Move master language to the first position
*
* @param LanguageInterface[] $languages
*
* @return LanguageInterface[]
*/
protected function promoteMasterLanguage(array $languages)
{
$index = array_search($this->masterLocale, $languages);

if (false !== $index) {

unset($languages[$index]);
array_unshift($languages, $this->masterLocale);
}

return $languages;
}
}

0 comments on commit 3d93272

Please sign in to comment.