From 4962b49ef4b2484fbb2a36b3104ba73dc09c7d98 Mon Sep 17 00:00:00 2001 From: Tomasz Szulc Date: Sat, 13 Feb 2016 10:31:32 +0100 Subject: [PATCH] Added configureIfNeeded method so there is no necessity to call configure() if you want to configure it for main bundle --- Swifternalization/Swifternalization.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Swifternalization/Swifternalization.swift b/Swifternalization/Swifternalization.swift index c637db0..16d328f 100644 --- a/Swifternalization/Swifternalization.swift +++ b/Swifternalization/Swifternalization.swift @@ -35,6 +35,12 @@ final public class Swifternalization { */ private var translations = [Translation]() + /** + Determine whether Swifternalization is configured. + It should be considered configured after `load(bundle:)` method is called. + */ + private var configured = false + // MARK: Public Methods /** @@ -46,6 +52,16 @@ final public class Swifternalization { sharedInstance.load(bundle) } + /** + Configures Swifternalization if you didn't do that before calling + `localizedString...` methods. + */ + private class func configureIfNeeded(bundle: NSBundle = NSBundle.mainBundle()) { + if sharedInstance.configured == false { + configure(bundle) + } + } + /** Get localized value for a key. @@ -83,6 +99,9 @@ final public class Swifternalization { specified or `key` if `defaultValue` is not specified. */ public class func localizedString(key: String, stringValue: String, fittingWidth: Int? = nil, defaultValue: String? = nil, comment: String? = nil) -> String { + + configureIfNeeded() + /** Filter translations and get only these that match passed `key`. In ideal case when all is correctly filled by a developer it should be @@ -165,6 +184,7 @@ final public class Swifternalization { // Store processed translations in `translations` variable for future use. translations = LoadedTranslationsProcessor.processTranslations(baseTranslations, preferedLanguageTranslations: languageTranslations, sharedExpressions: expressions) + configured = true } /**