11<?php
2- namespace Temporal ;
2+ namespace Temporal {
33
44class I18n {
55 public static $ SUPPORTED_LANGS = [
@@ -95,7 +95,6 @@ public static function init() {
9595
9696 private static function detectBrowserLanguage ($ header ) {
9797 $ languages = [];
98- // Format: tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6
9998 if (preg_match_all ('/([a-z]{1,8}(?:-[a-z]{1,8})?)\s*(?:;\s*q\s*=\s*(1|0\.[0-9]+))?/i ' , $ header , $ matches )) {
10099 foreach ($ matches [1 ] as $ index => $ langCode ) {
101100 $ q = !empty ($ matches [2 ][$ index ]) ? (float )$ matches [2 ][$ index ] : 1.0 ;
@@ -134,22 +133,16 @@ public static function loadLanguages($lang) {
134133 public static function get ($ key , $ params = []) {
135134 self ::init ();
136135
137- // 1. Direct translation
138136 if (isset (self ::$ translations [$ key ])) {
139137 $ text = self ::$ translations [$ key ];
140- }
141- // 2. English fallback
142- elseif (isset (self ::$ fallbackTranslations [$ key ])) {
138+ } elseif (isset (self ::$ fallbackTranslations [$ key ])) {
143139 $ text = self ::$ fallbackTranslations [$ key ];
144- }
145- // 3. Key as string
146- else {
140+ } else {
147141 $ text = $ key ;
148142 }
149143
150144 if (!empty ($ params )) {
151145 if (is_array ($ params )) {
152- // Check for named placeholders {name}
153146 $ hasNamed = false ;
154147 foreach ($ params as $ k => $ v ) {
155148 if (is_string ($ k )) {
@@ -184,13 +177,9 @@ public static function getSupportedLanguages() {
184177
185178 public static function getAll () {
186179 self ::init ();
187- // Merge fallback and active translations
188180 return array_merge (self ::$ fallbackTranslations , self ::$ translations );
189181 }
190182
191- /**
192- * Generates a URL to switch language while preserving current query parameters
193- */
194183 public static function getSwitchUrl ($ targetLang ) {
195184 $ params = $ _GET ;
196185 $ params ['lang ' ] = $ targetLang ;
@@ -199,12 +188,16 @@ public static function getSwitchUrl($targetLang) {
199188 }
200189}
201190
202- // Global translation helper
203- if (!function_exists ('__ ' )) {
204- function __ ($ key , ...$ params ) {
205- return \Temporal \I18n::get ($ key , $ params );
191+ // Auto-initialize on load
192+ I18n::init ();
193+
194+ } // End namespace Temporal
195+
196+ namespace {
197+ // Global translation helper in root namespace
198+ if (!function_exists ('__ ' )) {
199+ function __ ($ key , ...$ params ) {
200+ return \Temporal \I18n::get ($ key , $ params );
201+ }
206202 }
207203}
208-
209- // Auto-initialize on load
210- \Temporal \I18n::init ();
0 commit comments