944d91c1df
When an object has a "main" many relation with related "things" (objects, parameters, ...), the method names are normalized: * get() * set() * all() * replace() * remove() * clear() * isEmpty() * add() * register() * count() * keys() The classes below follow this method naming convention: * BrowserKit\CookieJar -> Cookie * BrowserKit\History -> Request * Console\Application -> Command * Console\Application\Helper\HelperSet -> HelperInterface * DependencyInjection\Container -> services * DependencyInjection\ContainerBuilder -> services * DependencyInjection\ParameterBag\ParameterBag -> parameters * DependencyInjection\ParameterBag\FrozenParameterBag -> parameters * DomCrawler\Form -> FormField * EventDispatcher\Event -> parameters * Form\FieldGroup -> Field * HttpFoundation\HeaderBag -> headers * HttpFoundation\ParameterBag -> parameters * HttpFoundation\Session -> attributes * HttpKernel\Profiler\Profiler -> DataCollectorInterface * Routing\RouteCollection -> Route * Security\Authentication\AuthenticationProviderManager -> AuthenticationProviderInterface * Templating\Engine -> HelperInterface * Translation\MessageCatalogue -> messages The usage of these methods are only allowed when it is clear that there is a main relation: * a CookieJar has many Cookies; * a Container has many services and many parameters (as services is the main relation, we use the naming convention for this relation); * a Console Input has many arguments and many options. There is no "main" relation, and so the naming convention does not apply. For many relations where the convention does not apply, the following methods must be used instead (where XXX is the name of the related thing): * get() -> getXXX() * set() -> setXXX() * all() -> getXXXs() * replace() -> setXXXs() * remove() -> removeXXX() * clear() -> clearXXX() * isEmpty() -> isEmptyXXX() * add() -> addXXX() * register() -> registerXXX() * count() -> countXXX() * keys()
67 lines
1.9 KiB
PHP
67 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Symfony\Component\Translation\Loader;
|
|
|
|
use Symfony\Component\Translation\MessageCatalogue;
|
|
|
|
/*
|
|
* This file is part of the Symfony framework.
|
|
*
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
*
|
|
* This source file is subject to the MIT license that is bundled
|
|
* with this source code in the file LICENSE.
|
|
*/
|
|
|
|
/**
|
|
* ArrayLoader loads translations from a PHP array.
|
|
*
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
*/
|
|
class ArrayLoader implements LoaderInterface
|
|
{
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function load($resource, $locale, $domain = 'messages')
|
|
{
|
|
$this->flatten($resource);
|
|
$catalogue = new MessageCatalogue($locale);
|
|
$catalogue->add($resource, $domain);
|
|
|
|
return $catalogue;
|
|
}
|
|
|
|
/**
|
|
* Flattens an nested array of translations
|
|
*
|
|
* The scheme used is:
|
|
* 'key' => array('key2' => array('key3' => 'value'))
|
|
* Becomes:
|
|
* 'key.key2.key3' => 'value'
|
|
*
|
|
* This function takes an array by reference and will modify it
|
|
*
|
|
* @param array $messages the array that will be flattened
|
|
* @param array $subnode current subnode being parsed, used internally for recursive calls
|
|
* @param string $path current path being parsed, used internally for recursive calls
|
|
*/
|
|
protected function flatten(array &$messages, array $subnode = null, $path = null)
|
|
{
|
|
if ($subnode === null) {
|
|
$subnode =& $messages;
|
|
}
|
|
foreach ($subnode as $key => $value) {
|
|
if (is_array($value)) {
|
|
$nodePath = $path ? $path.'.'.$key : $key;
|
|
$this->flatten($messages, $value, $nodePath);
|
|
if ($path === null) {
|
|
unset($messages[$key]);
|
|
}
|
|
} elseif ($path !== null) {
|
|
$messages[$path.'.'.$key] = $value;
|
|
}
|
|
}
|
|
}
|
|
}
|