6use Ubiquity\utils\base\UArray;
21 private $key =
'translations/';
23 private function getRootKey($locale =
null, $domain =
null) {
24 return $this->key . $locale ??
'' . $domain ??
'';
27 public function __construct($rootDir) {
28 $this->rootDir = $rootDir;
31 public function loadDomain($locale, $domain) {
33 $rootDirectory = $this->getRootDirectory ( $locale );
34 if (\file_exists ( $rootDirectory )) {
35 $filename = $rootDirectory . \DS . $domain .
'.php';
36 if (\file_exists ( $filename )) {
37 $messages = $this->loadFile ( $filename );
43 public function load($locale, $domain =
'*') {
44 $key = $this->getRootKey ( $locale, $domain );
45 if (CacheManager::$cache->exists ( $key )) {
46 return CacheManager::$cache->fetch ( $key );
49 $rootDirectory = $this->getRootDirectory ( $locale );
50 if (\file_exists ( $rootDirectory )) {
51 $files = UFileSystem::glob_recursive ( $rootDirectory . $domain .
'.php' );
52 foreach ( $files as $file ) {
53 if (\file_exists ( $file )) {
54 $name = \basename ( $file,
'.php' );
55 Logger::info (
'Translate',
'Loading ' . $locale .
'.' . $domain .
' from file ' . $name,
'load', [ \get_class () ] );
56 $messages [$name] = $this->loadFile ( $file );
59 $this->flatten ( $messages );
60 CacheManager::$cache->store ( $key, $messages );
68 public function clearCache($locale =
null, $domain =
null) {
69 if (isset ( $locale )) {
70 CacheManager::$cache->remove ( $this->getRootKey ( $locale, $domain ) );
72 CacheManager::$cache->clearCache ( $this->getRootKey ( $locale, $domain ) );
76 protected function loadFile($filename) {
77 return include $filename;
80 private function getRootDirectory($locale) {
81 return $this->rootDir . \DS . $locale . \DS;
84 private function getDirectory($domain, &$filename) {
85 $parts = \explode (
'.', $domain );
86 $filename = \array_pop ( $parts ) .
".php";
87 return \implode ( \DS, $parts );
104 private function flatten(array &$messages, array $subnode =
null, $path =
null) {
105 if (
null === $subnode) {
106 $subnode = &$messages;
108 foreach ( $subnode as $key => $value ) {
109 if (\is_array ( $value )) {
110 $nodePath = $path ? $path .
'.' . $key : $key;
111 $this->flatten ( $messages, $value, $nodePath );
112 if (
null === $path) {
113 unset ( $messages [$key] );
115 } elseif (
null !== $path) {
116 $messages [$path .
'.' . $key] = $value;
121 public function save($messages, $locale, $domain) {
122 $content =
"<?php\nreturn " . UArray::asPhpArray ( $messages,
'array' ) .
';';
124 $path = $this->getRootDirectory ( $locale ) . $this->getDirectory ( $domain, $filename );
125 if (UFileSystem::safeMkdir ( $path )) {
126 if (@\file_put_contents ( $path . \DS . $filename, $content, LOCK_EX ) ===
false) {
127 throw new \Exception (
"Unable to write cache file: {$filename}" );
130 throw new \Exception (
"Unable to create folder : {$path}" );
138 public function getRootDir() {
139 return $this->rootDir;
142 public function getDomains($locale) {
144 $rootDirectory = $this->getRootDirectory ( $locale );
145 if (\file_exists ( $rootDirectory )) {
146 $files = UFileSystem::glob_recursive ( $rootDirectory .
'*.php' );
147 foreach ( $files as $file ) {
148 $domains [] = \basename ( $file,
'.php' );
154 public function cacheExists($locale, $domain =
'*') {
155 $key = $this->getRootKey ( $locale, $domain );
156 return CacheManager::$cache->exists ( $key );
Manager for caches (Router, Rest, models).
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.
Translations loader interface.