@@ -28,6 +28,11 @@ class CustomTranslateExtension extends AbstractExtension
2828 * @var array
2929 */
3030 protected static $ translations = [];
31+
32+ /**
33+ * @var array
34+ */
35+ protected static $ translationsKeys = [];
3136 /**
3237 * @var string
3338 */
@@ -44,17 +49,30 @@ class CustomTranslateExtension extends AbstractExtension
4449 /**
4550 * @return array|mixed
4651 */
47- protected static function extractBaseTranslations ()
52+ protected static function extractBaseTranslations ($ locale = null )
4853 {
54+ $ locale = $ locale ?? self ::$ locale ;
4955 // Gather always the base translations
5056 $ standardTranslations = [];
51- self ::$ filename = implode (DIRECTORY_SEPARATOR , [LOCALE_DIR , 'custom ' , self :: $ locale . '.json ' ]);
57+ self ::$ filename = implode (DIRECTORY_SEPARATOR , [LOCALE_DIR , 'custom ' , $ locale . '.json ' ]);
5258 if (file_exists (self ::$ filename )) {
5359 $ standardTranslations = json_decode (file_get_contents (self ::$ filename ), true );
5460 }
5561 return $ standardTranslations ;
5662 }
5763
64+ /**
65+ * @param string $locale
66+ * @return void
67+ */
68+ protected static function generateTranslationsKeys ($ locale ) {
69+ self ::$ translationsKeys [$ locale ] = [];
70+ foreach (self ::$ translations [$ locale ] as $ key => $ value ) {
71+ $ tKey = mb_convert_case ($ key , MB_CASE_LOWER , "UTF-8 " );
72+ self ::$ translationsKeys [$ locale ][$ tKey ] = $ key ;
73+ }
74+ }
75+
5876 /**
5977 * @param string $customKey
6078 * @param bool $forceReload
@@ -64,20 +82,28 @@ protected static function translationsCheckLoad($customKey = null, $forceReload
6482 {
6583 Inspector::stats ('[translationsCheckLoad] Start checking translations load ' , Inspector::SCOPE_DEBUG );
6684 $ session = Security::getInstance ();
67- $ session_locale = $ session ->getSessionKey (I18nHelper::PSFS_SESSION_LANGUAGE_KEY );
85+ $ session_locale = $ session ->getSessionKey (I18nHelper::PSFS_SESSION_LOCALE_KEY ) ?? $ session -> getSessionKey (I18nHelper:: PSFS_SESSION_LANGUAGE_KEY );
6886 self ::$ locale = $ forceReload ? $ session_locale : I18nHelper::extractLocale ($ session_locale );
87+ $ locale = self ::$ locale ;
6988 $ version = $ session ->getSessionKey (self ::LOCALE_CACHED_VERSION );
7089 $ configVersion = self ::$ locale . '_ ' . Config::getParam ('cache.var ' , 'v1 ' );
7190 if ($ forceReload ) {
7291 Inspector::stats ('[translationsCheckLoad] Force translations reload ' , Inspector::SCOPE_DEBUG );
7392 self ::dropInstance ();
7493 $ version = null ;
75- self ::$ translations = [];
94+ self ::$ translations [self ::$ locale ] = [];
95+ }
96+ if ((!array_key_exists ($ locale , self ::$ translations ) || count (self ::$ translations [$ locale ]) === 0 ) && strlen ($ locale ) === 2 ) {
97+ $ locale = $ locale . '_ ' . strtoupper ($ locale );
98+ if (array_key_exists ($ locale , self ::$ translations )) {
99+ self ::$ translations [self ::$ locale ] = self ::$ translations [$ locale ];
100+ self ::generateTranslationsKeys (self ::$ locale );
101+ }
76102 }
77- if ( count (self ::$ translations ) === 0 ) {
103+ if (! array_key_exists ( $ locale , self :: $ translations ) || count (self ::$ translations[ $ locale ] ) === 0 ) {
78104 Inspector::stats ('[translationsCheckLoad] Extracting translations ' , Inspector::SCOPE_DEBUG );
79105 self ::$ generate = (boolean )Config::getParam ('i18n.autogenerate ' , false );
80- if (null !== $ version && $ version === $ configVersion ) {
106+ if (null !== $ version && $ version === $ configVersion ) {
81107 Inspector::stats ('[translationsCheckLoad] Translations loaded from session ' , Inspector::SCOPE_DEBUG );
82108 self ::$ translations = $ session ->getSessionKey (self ::LOCALE_CACHED_TAG );
83109 } else {
@@ -88,12 +114,16 @@ protected static function translationsCheckLoad($customKey = null, $forceReload
88114 // If the project has custom translations, gather them
89115 if (null !== $ customKey ) {
90116 Logger::log ('[ ' . self ::class . '] Custom key detected: ' . $ customKey , LOG_INFO );
91- self ::$ filename = implode (DIRECTORY_SEPARATOR , [LOCALE_DIR , 'custom ' , $ customKey , self ::$ locale . '.json ' ]);
117+ self ::$ filename = implode (DIRECTORY_SEPARATOR , [LOCALE_DIR , 'custom ' , $ customKey , $ locale . '.json ' ]);
118+ } elseif (!empty ($ standardTranslations )) {
119+ self ::$ translations [$ locale ] = $ standardTranslations ;
120+ self ::generateTranslationsKeys ($ locale );
92121 }
93122 // Finally we merge base and custom translations to complete all the i18n set
94123 if (file_exists (self ::$ filename )) {
95- Logger::log ('[ ' . self ::class . '] Custom locale detected: ' . $ customKey . ' [ ' . self ::$ locale . '] ' , LOG_INFO );
96- self ::$ translations = array_merge ($ standardTranslations , json_decode (file_get_contents (self ::$ filename ), true ));
124+ Logger::log ('[ ' . self ::class . '] Custom locale detected: ' . $ customKey . ' [ ' . $ locale . '] ' , LOG_INFO );
125+ self ::$ translations [$ locale ] = array_merge ($ standardTranslations , json_decode (file_get_contents (self ::$ filename ), true ));
126+ self ::generateTranslationsKeys ($ locale );
97127 $ session ->setSessionKey (self ::LOCALE_CACHED_TAG , self ::$ translations );
98128 $ session ->setSessionKey (self ::LOCALE_CACHED_VERSION , $ configVersion );
99129 } elseif (null !== $ customKey ) {
@@ -140,12 +170,21 @@ public function getName()
140170 */
141171 public static function _ ($ message , $ customKey = null , $ forceReload = false )
142172 {
143- if (0 === count (self ::$ translations ) || $ forceReload ) {
173+ if (0 === count (self ::$ translations ) || $ forceReload ) {
144174 self ::translationsCheckLoad ($ customKey , $ forceReload );
145175 }
146- if (is_array (self ::$ translations ) && array_key_exists ($ message , self ::$ translations )) {
147- $ translation = self ::$ translations [$ message ];
148- } else {
176+ // Set default translation to catch missing strings
177+ $ isDebugMode = (bool )Config::getParam ('debug ' , false );
178+ $ translation = (bool )Config::getParam ('debug ' , false ) ? 'MISSING_TRANSLATION - ' . self ::$ locale : $ message ;
179+ // Check if the message is already translated ignoring the string case
180+ $ key = mb_convert_case ($ message , MB_CASE_LOWER , "UTF-8 " );
181+ if (array_key_exists (self ::$ locale , self ::$ translationsKeys ) && array_key_exists ($ key , self ::$ translationsKeys [self ::$ locale ])) {
182+ $ message = self ::$ translationsKeys [self ::$ locale ][$ key ];
183+ }
184+ // Check if exists
185+ if (array_key_exists (self ::$ locale , self ::$ translations ) && array_key_exists ($ message , self ::$ translations [self ::$ locale ])) {
186+ $ translation = self ::$ translations [self ::$ locale ][$ message ];
187+ } else if (!$ forceReload && !$ isDebugMode ) {
149188 $ translation = gettext ($ message );
150189 }
151190 if (self ::$ generate ) {
@@ -160,8 +199,13 @@ public static function _($message, $customKey = null, $forceReload = false)
160199 */
161200 protected static function generate ($ message , $ translation )
162201 {
202+ if (!array_key_exists (self ::$ locale , self ::$ translations )) {
203+ self ::$ translations [self ::$ locale ] = [];
204+ self ::$ translationsKeys [self ::$ locale ] = [];
205+ }
163206 if (!array_key_exists ($ message , self ::$ translations )) {
164- self ::$ translations [$ message ] = $ translation ;
207+ self ::$ translations [self ::$ locale ][$ message ] = $ translation ;
208+ self ::$ translationsKeys [self ::$ locale ][mb_convert_case ($ message , MB_CASE_LOWER , "UTF-8 " )] = $ message ;
165209 }
166210 file_put_contents (self ::$ filename , json_encode (array_unique (self ::$ translations ), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ));
167211 }
0 commit comments