From fd6446c9f1382db76643dde1b61e2be9031257b9 Mon Sep 17 00:00:00 2001 From: Narayan Sainaney Date: Mon, 4 Jul 2016 11:10:17 -0700 Subject: [PATCH] Fixed lookup for languageBundle. For cases like "en_CA", if "en_CA" is not found, this now uses the "en" language strings instead of showing empty strings --- SwiftMoment/SwiftMoment/MomentFromNow.swift | 26 +++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/SwiftMoment/SwiftMoment/MomentFromNow.swift b/SwiftMoment/SwiftMoment/MomentFromNow.swift index 477e9db..0f20edc 100644 --- a/SwiftMoment/SwiftMoment/MomentFromNow.swift +++ b/SwiftMoment/SwiftMoment/MomentFromNow.swift @@ -105,15 +105,27 @@ extension Moment { guard let bundle = NSBundle(URL: path) else { return "" } - - let localeIdentifer = self.locale.localeIdentifier - guard let languagePath = bundle.pathForResource(localeIdentifer, ofType: "lproj"), - languageBundle = NSBundle(path: languagePath) - else { - return "" + + if let languageBundle = getLanguageBundle(bundle) { + return languageBundle.localizedStringForKey(key, value: "", table: "NSDateTimeAgo") } + + return "" + } + + private func getLanguageBundle(bundle: NSBundle) -> NSBundle? { + let localeIdentifer = self.locale.localeIdentifier + if let languagePath = bundle.pathForResource(localeIdentifer, ofType: "lproj") { + return NSBundle(path: languagePath) + } + + let langDict = NSLocale.componentsFromLocaleIdentifier(localeIdentifer) + let languageCode = langDict["kCFLocaleLanguageCodeKey"] + if let languagePath = bundle.pathForResource(languageCode, ofType: "lproj") { + return NSBundle(path: languagePath) + } - return languageBundle.localizedStringForKey(key, value: "", table: "NSDateTimeAgo") + return nil } private func getLocaleFormatUnderscoresWithValue(value: Double) -> String {