@@ -120,12 +120,23 @@ extension String {
120
120
return modifiedString. components ( separatedBy: stop)
121
121
}
122
122
123
- func localized( forLanguageCode languageCode: String ) -> String {
124
- guard let path = Bundle . main. path ( forResource: formatLanguageCode ( languageCode) , ofType: " lproj " ) ,
125
- let bundle = Bundle ( path: path) else {
126
- return self // Return the original string if no localization is found
123
+ func localized( forLanguageCode languageCode: String ) -> String ? {
124
+ let formattedLanguageCode = formatLanguageCode ( languageCode)
125
+
126
+ // Try with the full language code first (e.g., "en-US")
127
+ if let path = Bundle . main. path ( forResource: formattedLanguageCode, ofType: " lproj " ) ,
128
+ let bundle = Bundle ( path: path) {
129
+ return NSLocalizedString ( self , bundle: bundle, comment: " " )
130
+ }
131
+
132
+ // If not found, try with only the language part (e.g., "en")
133
+ let primaryLanguageCode = onlyFirstLanguageCode ( formattedLanguageCode)
134
+ if let path = Bundle . main. path ( forResource: primaryLanguageCode, ofType: " lproj " ) ,
135
+ let bundle = Bundle ( path: path) {
136
+ return NSLocalizedString ( self , bundle: bundle, comment: " " )
127
137
}
128
- return NSLocalizedString ( self , bundle: bundle, comment: " " )
138
+
139
+ return nil // Return nil if no localization is found
129
140
}
130
141
131
142
private func formatLanguageCode( _ code: String ) -> String {
@@ -139,6 +150,11 @@ extension String {
139
150
return formattedCode
140
151
}
141
152
153
+ private func onlyFirstLanguageCode( _ code: String ) -> String {
154
+ let parts = code. split ( separator: " - " )
155
+ return parts [ 0 ] . lowercased ( )
156
+ }
157
+
142
158
var md5 : String {
143
159
let data = Data ( self . utf8)
144
160
let hash = data. withUnsafeBytes { ( bytes: UnsafeRawBufferPointer ) -> [ UInt8 ] in
0 commit comments