@@ -25,6 +25,14 @@ class Locale
2525 */
2626 public $ default ;
2727
28+ /**
29+ * Fallback locale. Used when specific or default locale is missing translation.
30+ * Should always be set to locale that includes all translations.
31+ *
32+ * @var string|null
33+ */
34+ public $ fallback = null ;
35+
2836 /**
2937 * Get list of configured languages
3038 *
@@ -54,7 +62,7 @@ public static function setLanguageFromArray(string $name, array $translations):
5462 */
5563 public static function setLanguageFromJSON (string $ name , string $ path ): void
5664 {
57- if (! file_exists ($ path )) {
65+ if (! file_exists ($ path ) && self :: $ exceptions ) {
5866 throw new Exception ('Translation file not found. ' );
5967 }
6068
@@ -65,13 +73,31 @@ public static function setLanguageFromJSON(string $name, string $path): void
6573
6674 public function __construct (string $ default )
6775 {
68- if (! \array_key_exists ($ default , self ::$ language )) {
76+ if (! \array_key_exists ($ default , self ::$ language ) && self :: $ exceptions ) {
6977 throw new Exception ('Locale not found ' );
7078 }
7179
7280 $ this ->default = $ default ;
7381 }
7482
83+ /**
84+ * Change fallback Locale
85+ *
86+ * @param $name
87+ *
88+ * @throws Exception
89+ */
90+ public function setFallback (string $ name ): self
91+ {
92+ if (! \array_key_exists ($ name , self ::$ language ) && self ::$ exceptions ) {
93+ throw new Exception ('Locale not found ' );
94+ }
95+
96+ $ this ->fallback = $ name ;
97+
98+ return $ this ;
99+ }
100+
75101 /**
76102 * Change Default Locale
77103 *
@@ -81,7 +107,7 @@ public function __construct(string $default)
81107 */
82108 public function setDefault (string $ name ): self
83109 {
84- if (! \array_key_exists ($ name , self ::$ language )) {
110+ if (! \array_key_exists ($ name , self ::$ language ) && self :: $ exceptions ) {
85111 throw new Exception ('Locale not found ' );
86112 }
87113
@@ -101,17 +127,22 @@ public function setDefault(string $name): self
101127 */
102128 public function getText (string $ key , array $ placeholders = [])
103129 {
104- $ default = '{{ ' .$ key .'}} ' ;
130+ $ defaultExists = \array_key_exists ($ key , self ::$ language [$ this ->default ]);
131+ $ fallbackExists = \array_key_exists ($ key , self ::$ language [$ this ->fallback ?? '' ] ?? []);
105132
106- if (! \array_key_exists ($ key , self ::$ language [$ this ->default ])) {
107- if (self ::$ exceptions ) {
108- throw new Exception ('Key named " ' .$ key .'" not found ' );
109- }
133+ $ translation = '{{ ' .$ key .'}} ' ;
110134
111- return $ default ;
135+ if ($ fallbackExists ) {
136+ $ translation = self ::$ language [$ this ->fallback ?? '' ][$ key ];
112137 }
113138
114- $ translation = self ::$ language [$ this ->default ][$ key ];
139+ if ($ defaultExists ) {
140+ $ translation = self ::$ language [$ this ->default ][$ key ];
141+ }
142+
143+ if (! $ defaultExists && ! $ fallbackExists && self ::$ exceptions ) {
144+ throw new Exception ('Key named " ' .$ key .'" not found ' );
145+ }
115146
116147 foreach ($ placeholders as $ placeholderKey => $ placeholderValue ) {
117148 $ translation = str_replace ('{{ ' .$ placeholderKey .'}} ' , (string ) $ placeholderValue , $ translation );
0 commit comments