Skip to content

toRelative can't correctly map language. #754

@g761007

Description

@g761007

ex. Locale (zh-Hant)

let localeId = (locale.collatorIdentifier ?? Locales.english.toLocale().collatorIdentifier!) // zh-Hant

case in RelativeFormatterLanguage is "zh_Hant"

This case makes it impossible to map the correct RelativeFormatterLanguage , and it map to the case zh

private func tableForLocale(_ locale: Locale) -> RelativeFormatterLanguage {
let localeId = (locale.collatorIdentifier ?? Locales.english.toLocale().collatorIdentifier!)
if let lang = RelativeFormatterLanguage(rawValue: localeId) {
return lang
}
guard let fallbackFlavours = RelativeFormatterLanguage(rawValue: localeId.components(separatedBy: "_").first!) ??
RelativeFormatterLanguage(rawValue: localeId.components(separatedBy: "-").first!) else {
return tableForLocale(Locales.english.toLocale()) // fallback not found, return english
}
return fallbackFlavours // return fallback
}

I think the solution

let localeId = (locale.collatorIdentifier ?? Locales.english.toLocale().collatorIdentifier!).replacingOccurrences(of: "-", with: "_")

or write RelativeFormatterLanguage init:rawValue

public init?(rawValue: String) {
  let rawValue = rawValue.replacingOccurrences(of: "-", with: "_")
  self = RelativeFormatter.allLanguages.first(where: { $0.rawValue == rawValue }) ?? .en
  if rawValue == "bs_Cyrl" {
    self = .bs_Cyrl
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions