本地化字符串比较求助:英文数组与缅甸语本地化字符串匹配
First, let’s align on the core problem: you’ve got an array of English keys (keyArray) that map to Burmese localized strings via your app’s localization files, and you want to filter this array to show only entries whose Burmese translation matches the user’s search input. Apple’s default string comparison methods might not handle Burmese script nuances out of the box, so here are targeted fixes to get this working:
1. Double-Check You’re Comparing the Correct String
It’s easy to accidentally compare the search input directly to the English keys instead of their Burmese counterparts. Make sure you’re fetching the localized string for each key before running the comparison:
// Example of your String localization extension (adjust if yours differs) extension String { var localized: String { NSLocalizedString(self, comment: "") } } // Correct filtering logic: compare search input to the Burmese localized string let searchText = searchBar.text ?? "" searchResultList = keyArray.filter { key in let burmeseTranslation = key.localized return burmeseTranslation.localizedStandardContains(searchText) }
2. Use Locale-Aware Comparison
Burmese has unique script characteristics, so explicitly specifying the Burmese locale ensures the comparison respects the language’s rules:
let burmeseLocale = Locale(identifier: "my_MM") searchResultList = keyArray.filter { key in let localizedString = key.localized let comparisonOptions: NSString.CompareOptions = [.caseInsensitive, .diacriticInsensitive] return localizedString.range(of: searchText, options: comparisonOptions, locale: burmeseLocale) != nil }
localizedStandardContains (from the first example) uses the system’s current locale, but hardcoding my_MM guarantees consistency even if the user’s device locale isn’t set to Burmese.
3. Normalize Strings to Fix Unicode Mismatches
Burmese script can have multiple Unicode representations for the same visual character (e.g., combining marks). Normalizing both the search input and localized string to a consistent form eliminates these mismatches:
func normalizedBurmeseString(_ string: String) -> String { return string.folding( options: [.diacriticInsensitive, .caseInsensitive], locale: Locale(identifier: "my_MM") ) } searchResultList = keyArray.filter { key in let normalizedTranslation = normalizedBurmeseString(key.localized) let normalizedSearch = normalizedBurmeseString(searchText) return normalizedTranslation.contains(normalizedSearch) }
4. Verify Your Localization Files
Ensure your Burmese Localizable.strings file (my.lproj/Localizable.strings) is correctly formatted with no typos or missing entries. A missing key will fall back to the English string, breaking your search logic. Example valid entry:
"welcome_message" = "မင်္ဂလာပါ!";
If none of these resolve the issue, could you share a bit more context? Like:
- Is the search input in Burmese or English?
- Are you getting no results at all, or incorrect matches?
- The full code of your
localizedextension and filtering logic.
内容的提问来源于stack exchange,提问作者Antony

