Updated the "name(for:)" method for the ContentViewModel view model in the Sample app target to append the region flag emoji to the locale name.
This commit is contained in:
@@ -226,6 +226,36 @@ struct ContentViewModelTests {
|
||||
|
||||
}
|
||||
|
||||
// MARK: Name
|
||||
|
||||
@MainActor
|
||||
@Suite("Name")
|
||||
struct Name {
|
||||
|
||||
@Test
|
||||
func `name pairs the localized language name with the region flag`() {
|
||||
let model = Model(preinstaller: PreinstallingMock())
|
||||
|
||||
#expect(model.name(for: .english) == "\(languageName("en")) 🇺🇸")
|
||||
#expect(model.name(for: .dutch) == "\(languageName("nl")) 🇳🇱")
|
||||
}
|
||||
|
||||
@Test
|
||||
func `name omits the flag for a locale without a region`() {
|
||||
let model = Model(preinstaller: PreinstallingMock())
|
||||
|
||||
#expect(!model.name(for: Locale(identifier: "en")).containsFlag)
|
||||
}
|
||||
|
||||
@Test
|
||||
func `name omits the flag for a numeric region`() {
|
||||
let model = Model(preinstaller: PreinstallingMock())
|
||||
|
||||
#expect(!model.name(for: Locale(identifier: "es-419")).containsFlag)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Constants
|
||||
@@ -238,3 +268,20 @@ private extension Locale {
|
||||
/// Another supported locale to preinstall in the tests.
|
||||
static let english = Locale(identifier: "en-US")
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
/// The capitalized, localized language name for a language code, mirroring how the model builds the language part of a locale's name.
|
||||
///
|
||||
/// - Parameter code: The language code to name.
|
||||
/// - Returns: The localized language name in the current locale, capitalized.
|
||||
private func languageName(_ code: String) -> String {
|
||||
(Locale.current.localizedString(forLanguageCode: code) ?? code).localizedCapitalized
|
||||
}
|
||||
|
||||
private extension String {
|
||||
/// Whether the string contains a Regional Indicator Symbol scalar — part of a flag emoji.
|
||||
var containsFlag: Bool {
|
||||
unicodeScalars.contains { $0.value >= 0x1F1E6 && $0.value <= 0x1F1FF }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user