Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toRelative can't correctly map language. #754

Open
g761007 opened this issue Oct 6, 2020 · 1 comment
Open

toRelative can't correctly map language. #754

g761007 opened this issue Oct 6, 2020 · 1 comment

Comments

@g761007
Copy link

g761007 commented Oct 6, 2020

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
  }

@kf99916
Copy link

kf99916 commented Jun 16, 2021

Fixed in #778
Create a locale id fallback rule to support different locale format as we can as possible:
collatorIdentifier: zh-Hant-TW -> zh_Hant_TW -> zh-Hant -> zh_Hant -> zh -> en

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants