| 214 | } |
| 215 | |
| 216 | void LocalizationService::OnLocalizationChanged() |
| 217 | { |
| 218 | PROFILE_CPU(); |
| 219 | PROFILE_MEM(Localization); |
| 220 | |
| 221 | Instance.LocalizedStringTables.Clear(); |
| 222 | Instance.FallbackStringTables.Clear(); |
| 223 | const StringView en(TEXT("en")); |
| 224 | |
| 225 | // Collect all localization tables into mapping locale -> tables |
| 226 | auto& settings = *LocalizationSettings::Get(); |
| 227 | Dictionary<String, Array<AssetReference<LocalizedStringTable>, InlinedAllocation<32>>> tables; |
| 228 | for (auto& e : settings.LocalizedStringTables) |
| 229 | { |
| 230 | auto table = e.Get(); |
| 231 | if (table && !table->WaitForLoaded()) |
| 232 | { |
| 233 | tables[table->Locale].Add(table); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // Pick localization tables for a current language |
| 238 | auto* table = tables.TryGet(Instance.CurrentLanguage.GetName()); |
| 239 | if (!table) |
| 240 | { |
| 241 | // Try using parent culture (eg. en if en-GB is missing) |
| 242 | const CultureInfo parentLanguage(Instance.CurrentLanguage.GetParentLCID()); |
| 243 | if (parentLanguage.GetName().HasChars()) |
| 244 | table = tables.TryGet(parentLanguage.GetName()); |
| 245 | if (!table) |
| 246 | { |
| 247 | // Fallback to Default |
| 248 | table = tables.TryGet(settings.DefaultFallbackLanguage); |
| 249 | if (!table) |
| 250 | { |
| 251 | // Fallback to English |
| 252 | table = tables.TryGet(en); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Apply localization table |
| 258 | if (table) |
| 259 | { |
| 260 | String locale; |
| 261 | for (auto& e : tables) |
| 262 | { |
| 263 | if (&e.Value == table) |
| 264 | { |
| 265 | locale = e.Key; |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | LOG(Info, "Using localization for {0}", locale); |
| 270 | Instance.LocalizedStringTables.Add(table->Get(), table->Count()); |
| 271 | if (locale != settings.DefaultFallbackLanguage || locale != en) |
| 272 | { |
| 273 | // Cache fallback language tables to support additional text resolving in case of missing entries in the current language |
no test coverage detected