Skip to content

Commit 81fa578

Browse files
kokspflanzesvycka
authored andcommitted
added a view-helper to get the primary-language
1 parent 23165f8 commit 81fa578

File tree

5 files changed

+191
-11
lines changed

5 files changed

+191
-11
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ in the `html` tag:
110110
Inject the detected language here with the following code:
111111

112112
```
113-
<html lang="<?= Locale::getPrimaryLanguage(Locale::getDefault())?>">
113+
<html lang="<?= $this->primaryLanguage()?>">
114114
```
115115

116116
### Disable UriPathStrategy in PHPUNIT

config/module.config.php

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
<?php
22

33
use SlmLocale\Locale\Detector;
4-
use SlmLocale\Service\DetectorFactory;
5-
use SlmLocale\Service\LocaleMenuViewHelperFactory;
6-
use SlmLocale\Service\LocaleUrlViewHelperFactory;
4+
use SlmLocale\Service;
75
use SlmLocale\Strategy\Factory\StrategyPluginManagerFactory;
86
use SlmLocale\Strategy\StrategyPluginManager;
9-
use SlmLocale\View\Helper\LocaleMenu;
10-
use SlmLocale\View\Helper\LocaleUrl;
7+
use SlmLocale\View\Helper;
118

129
/**
1310
* Copyright (c) 2012-2013 Jurian Sluiman.
@@ -56,18 +53,20 @@
5653
'service_manager' => [
5754
'factories' => [
5855
StrategyPluginManager::class => StrategyPluginManagerFactory::class,
59-
Detector::class => DetectorFactory::class,
56+
Detector::class => Service\DetectorFactory::class,
6057
],
6158
],
6259

6360
'view_helpers' => [
6461
'aliases' => [
65-
'localeUrl' => LocaleUrl::class,
66-
'localeMenu' => LocaleMenu::class,
62+
'localeUrl' => Helper\LocaleUrl::class,
63+
'localeMenu' => Helper\LocaleMenu::class,
64+
'primaryLanguage' => Helper\PrimaryLanguage::class,
6765
],
6866
'factories' => [
69-
LocaleUrl::class => LocaleUrlViewHelperFactory::class,
70-
LocaleMenu::class => LocaleMenuViewHelperFactory::class,
67+
Helper\LocaleUrl::class => Service\LocaleUrlViewHelperFactory::class,
68+
Helper\LocaleMenu::class => Service\LocaleMenuViewHelperFactory::class,
69+
Helper\PrimaryLanguage::class => Service\PrimaryLanguageHelperFactory::class,
7170
],
7271
],
7372
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2012-2019 Jurian Sluiman.
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* * Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* * Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
*
18+
* * Neither the names of the copyright holders nor the names of the
19+
* contributors may be used to endorse or promote products derived
20+
* from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33+
* POSSIBILITY OF SUCH DAMAGE.
34+
*
35+
* @author Jurian Sluiman <[email protected]>
36+
* @copyright 2012-2013 Jurian Sluiman.
37+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
38+
* @link http://juriansluiman.nl
39+
*/
40+
namespace SlmLocale\Service;
41+
42+
use Interop\Container\ContainerInterface;
43+
use Locale;
44+
use SlmLocale\View\Helper\PrimaryLanguage;
45+
use Zend\ServiceManager\Factory\FactoryInterface;
46+
47+
final class PrimaryLanguageHelperFactory implements FactoryInterface
48+
{
49+
/**
50+
* @param ContainerInterface $container
51+
* @param string $requestedName
52+
* @param array|null $options
53+
* @return object|PrimaryLanguage
54+
*/
55+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
56+
{
57+
return new PrimaryLanguage(new Locale());
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2012-2019 Jurian Sluiman.
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* * Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* * Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
*
18+
* * Neither the names of the copyright holders nor the names of the
19+
* contributors may be used to endorse or promote products derived
20+
* from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33+
* POSSIBILITY OF SUCH DAMAGE.
34+
*
35+
* @author Jurian Sluiman <[email protected]>
36+
* @copyright 2012-2013 Jurian Sluiman.
37+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
38+
* @link http://juriansluiman.nl
39+
*/
40+
namespace SlmLocale\View\Helper;
41+
42+
use Locale;
43+
use Zend\View\Helper\AbstractHelper;
44+
45+
final class PrimaryLanguage extends AbstractHelper
46+
{
47+
/** @var Locale */
48+
protected $locale;
49+
50+
/**
51+
* PrimaryLanguage constructor.
52+
* @param Locale $locale
53+
*/
54+
public function __construct(Locale $locale)
55+
{
56+
$this->locale = $locale;
57+
}
58+
59+
/**
60+
* @return string
61+
*/
62+
public function __invoke()
63+
{
64+
return $this->locale->getPrimaryLanguage($this->locale->getDefault());
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2012-2019 Jurian Sluiman.
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* * Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* * Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
*
18+
* * Neither the names of the copyright holders nor the names of the
19+
* contributors may be used to endorse or promote products derived
20+
* from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33+
* POSSIBILITY OF SUCH DAMAGE.
34+
*
35+
* @author Jurian Sluiman <[email protected]>
36+
* @copyright 2012-2013 Jurian Sluiman.
37+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
38+
* @link http://juriansluiman.nl
39+
*/
40+
namespace SlmLocaleTest\View\Helper;
41+
42+
use Locale;
43+
use PHPUnit_Framework_TestCase as TestCase;
44+
use SlmLocale\View\Helper\PrimaryLanguage;
45+
46+
final class PrimaryLanguageTest extends TestCase
47+
{
48+
public function testInvoke()
49+
{
50+
$locale = new Locale();
51+
$locale->setDefault('de-DE');
52+
$primaryLanguage = new PrimaryLanguage($locale);
53+
54+
$this->assertSame('de', $primaryLanguage());
55+
}
56+
}

0 commit comments

Comments
 (0)