SwiftDate icon indicating copy to clipboard operation
SwiftDate copied to clipboard

toRelative can't correctly map language.

Open g761007 opened this issue 4 years ago • 1 comments

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

https://github.com/malcommac/SwiftDate/blob/89774a1e28826af1498693bcbdf9ff6037ec646f/Sources/SwiftDate/Formatters/RelativeFormatter/RelativeFormatter.swift#L32-L44

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
  }

g761007 avatar Oct 06 '20 10:10 g761007

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

kf99916 avatar Jun 16 '21 14:06 kf99916