1919
2020namespace FacturaScripts \Core \Base ;
2121
22- use FacturaScripts \Core \Plugins ;
2322use FacturaScripts \Core \Tools ;
24- use Symfony \Component \Translation \Loader \JsonFileLoader ;
25- use Symfony \Component \Translation \Translator as SymfonyTranslator ;
2623
2724/**
2825 * The Translator class manage all translations methods required for internationalization.
@@ -63,7 +60,7 @@ class Translator
6360 /**
6461 * The Symfony translator.
6562 *
66- * @var SymfonyTranslator
63+ * @var mixed
6764 */
6865 private static $ translator ;
6966
@@ -82,10 +79,6 @@ class Translator
8279 public function __construct (string $ langCode = '' )
8380 {
8481 $ this ->currentLang = empty ($ langCode ) ? $ this ->getDefaultLang () : $ langCode ;
85- if (self ::$ translator === null ) {
86- self ::$ translator = new symfonyTranslator ($ this ->currentLang );
87- self ::$ translator ->addLoader ('json ' , new JsonFileLoader ());
88- }
8982 }
9083
9184 /**
@@ -99,23 +92,7 @@ public function __construct(string $langCode = '')
9992 */
10093 public function customTrans (string $ langCode , string $ txt , array $ parameters = []): string
10194 {
102- if (!in_array ($ langCode , self ::$ languages )) {
103- $ this ->locateFiles ($ langCode );
104- }
105-
106- $ transKey = $ this ->getTransKey ($ txt );
107- $ catalogue = self ::$ translator ->getCatalogue ($ langCode );
108- if ($ catalogue ->has ($ transKey )) {
109- self ::$ usedStrings [$ transKey ] = $ catalogue ->get ($ transKey );
110- return self ::$ translator ->trans ($ transKey , $ parameters , null , $ langCode );
111- }
112-
113- self ::$ missingStrings [$ transKey ] = $ transKey ;
114- if ($ langCode === self ::FALLBACK_LANG ) {
115- return $ transKey ;
116- }
117-
118- return $ this ->customTrans (self ::FALLBACK_LANG , $ transKey , $ parameters );
95+ return $ txt ;
11996 }
12097
12198 /**
@@ -125,31 +102,7 @@ public function customTrans(string $langCode, string $txt, array $parameters = [
125102 */
126103 public function getAvailableLanguages (): array
127104 {
128- // obtenemos los directorios donde comprobar
129- $ folders = [FS_FOLDER . '/Core/Translation ' , FS_FOLDER . '/MyFiles/Translation ' ];
130- foreach (Plugins::enabled () as $ plugin ) {
131- $ folders [] = Plugins::folder () . '/ ' . $ plugin . '/Translation ' ;
132- }
133-
134- // obtenemos los idiomas según los directorios
135- $ languages = [];
136- foreach ($ folders as $ directory ) {
137- if (false === file_exists ($ directory ) || false === is_dir ($ directory )) {
138- continue ;
139- }
140-
141- foreach (scandir ($ directory , SCANDIR_SORT_ASCENDING ) as $ fileName ) {
142- if ($ fileName !== '. ' && $ fileName !== '.. ' && !is_dir ($ fileName ) && substr ($ fileName , -5 ) === '.json ' ) {
143- $ key = substr ($ fileName , 0 , -5 );
144- $ languages [$ key ] = $ this ->trans ('languages- ' . substr ($ fileName , 0 , -5 ));
145- }
146- }
147- }
148-
149- // ordenamos preservando las claves
150- asort ($ languages );
151-
152- return $ languages ;
105+ return [];
153106 }
154107
155108 /**
@@ -182,11 +135,6 @@ public function getMissingStrings(): array
182135
183136 public static function reload (): void
184137 {
185- if (self ::$ translator !== null ) {
186- self ::$ languages = [];
187- self ::$ translator = new symfonyTranslator (self ::$ defaultLang );
188- self ::$ translator ->addLoader ('json ' , new JsonFileLoader ());
189- }
190138 }
191139
192140 /**
@@ -202,36 +150,6 @@ public function trans(?string $txt, array $parameters = []): string
202150 return empty ($ txt ) ? '' : $ this ->customTrans ($ this ->currentLang , $ txt , $ parameters );
203151 }
204152
205- /**
206- *
207- * @param string $txt
208- *
209- * @return string
210- */
211- private function getTransKey (string $ txt ): string
212- {
213- $ specialKeys = [
214- 'AlbaranCliente ' => 'customer-delivery-note ' ,
215- 'AlbaranProveedor ' => 'supplier-delivery-note ' ,
216- 'FacturaCliente ' => 'customer-invoice ' ,
217- 'FacturaProveedor ' => 'supplier-invoice ' ,
218- 'PedidoCliente ' => 'customer-order ' ,
219- 'PedidoProveedor ' => 'supplier-order ' ,
220- 'PresupuestoCliente ' => 'customer-estimation ' ,
221- 'PresupuestoProveedor ' => 'supplier-estimation ' ,
222- 'AlbaranCliente-min ' => 'delivery-note ' ,
223- 'AlbaranProveedor-min ' => 'delivery-note ' ,
224- 'FacturaCliente-min ' => 'invoice ' ,
225- 'FacturaProveedor-min ' => 'invoice ' ,
226- 'PedidoCliente-min ' => 'order ' ,
227- 'PedidoProveedor-min ' => 'order ' ,
228- 'PresupuestoCliente-min ' => 'estimation ' ,
229- 'PresupuestoProveedor-min ' => 'estimation ' ,
230- ];
231-
232- return $ specialKeys [$ txt ] ?? $ txt ;
233- }
234-
235153 /**
236154 * Returns the strings used.
237155 *
@@ -259,59 +177,4 @@ public function setLang(string $langCode)
259177 {
260178 $ this ->currentLang = $ this ->findLang ($ langCode );
261179 }
262-
263- /**
264- * @param string $langCode
265- *
266- * @return string
267- */
268- private function findLang (string $ langCode ): string
269- {
270- // First match is with default lang? (Avoid match with variants)
271- if (0 === strpos ($ this ->getDefaultLang (), $ langCode )) {
272- return $ this ->getDefaultLang ();
273- }
274-
275- // If not, check with all available languages
276- $ finalKey = null ;
277- foreach (array_keys ($ this ->getAvailableLanguages ()) as $ key ) {
278- if ($ key === $ langCode ) {
279- return $ key ;
280- }
281-
282- if ($ finalKey === null && 0 === strpos ($ key , $ langCode )) {
283- $ finalKey = $ key ;
284- }
285- }
286-
287- return $ finalKey ?? $ this ->getDefaultLang ();
288- }
289-
290- /**
291- * Load the translation files following the priority system of FacturaScripts.
292- * In this case, the translator must be provided with the routes in reverse order.
293- *
294- * @param string $langCode
295- */
296- private function locateFiles (string $ langCode )
297- {
298- self ::$ languages [] = $ langCode ;
299-
300- $ coreFile = FS_FOLDER . '/Core/Translation/ ' . $ langCode . '.json ' ;
301- if (file_exists ($ coreFile )) {
302- self ::$ translator ->addResource ('json ' , $ coreFile , $ langCode );
303- }
304-
305- foreach (Plugins::enabled () as $ pluginName ) {
306- $ file2 = FS_FOLDER . '/Plugins/ ' . $ pluginName . '/Translation/ ' . $ langCode . '.json ' ;
307- if (file_exists ($ file2 )) {
308- self ::$ translator ->addResource ('json ' , $ file2 , $ langCode );
309- }
310- }
311-
312- $ myFile = FS_FOLDER . '/MyFiles/Translation/ ' . $ langCode . '.json ' ;
313- if (file_exists ($ myFile )) {
314- self ::$ translator ->addResource ('json ' , $ myFile , $ langCode );
315- }
316- }
317180}
0 commit comments