|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the WPSymfonyForm plugin. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2015-2016 LIN3S <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LIN3S\WPSymfonyForm\Translation; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Finder\Finder; |
|
15
|
|
|
use Symfony\Component\Translation\Loader\XliffFileLoader; |
|
16
|
|
|
use Symfony\Component\Translation\Translator as BaseTranslator; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Translator singleton class. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Gorka Laucirica <[email protected]> |
|
22
|
|
|
* @author Beñat Espiña <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class Translator |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* The instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @var self |
|
30
|
|
|
*/ |
|
31
|
|
|
private static $instance; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The factory method that returns the |
|
35
|
|
|
* instance of class in a singleton way. |
|
36
|
|
|
* |
|
37
|
|
|
* @return static |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function instance() |
|
40
|
|
|
{ |
|
41
|
|
|
if (!self::$instance) { |
|
42
|
|
|
self::$instance = static::create(); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return self::$instance; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Protected facade. |
|
50
|
|
|
* |
|
51
|
|
|
* @return Translator |
|
52
|
|
|
*/ |
|
53
|
|
|
protected static function create() |
|
54
|
|
|
{ |
|
55
|
|
|
$locale = 'es_ES'; |
|
56
|
|
|
if (defined('ICL_LANGUAGE_CODE')) { |
|
57
|
|
|
$locale = ICL_LANGUAGE_CODE; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$translator = new BaseTranslator($locale); |
|
61
|
|
|
$translator->addLoader('xlf', new XliffFileLoader()); |
|
62
|
|
|
|
|
63
|
|
|
$finder = new Finder(); |
|
64
|
|
|
$finder->files()->in(ABSPATH . '/../vendor/symfony/validator/Resources/translations/'); |
|
65
|
|
|
foreach ($finder as $validator) { |
|
66
|
|
|
$locale = str_replace('validators.', '', $validator->getRelativePathName()); |
|
67
|
|
|
$locale = str_replace('.xlf', '', $locale); |
|
68
|
|
|
|
|
69
|
|
|
$translator->addResource( |
|
70
|
|
|
'xlf', |
|
71
|
|
|
$validator->getRealpath(), |
|
72
|
|
|
$locale, |
|
73
|
|
|
'validators' |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $translator; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* This class cannot be instantiated. |
|
82
|
|
|
*/ |
|
83
|
|
|
private function __construct() |
|
84
|
|
|
{ |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|