diff --git a/Localize-Swift.podspec b/Localize-Swift.podspec index 82b0526..db5fbbc 100644 --- a/Localize-Swift.podspec +++ b/Localize-Swift.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = "Localize-Swift" - s.version = "1.7.1" + s.version = "1.7.2" s.summary = "Swift-friendly localization and i18n syntax with in-app language switching." # This description is used to generate tags and improve search results. diff --git a/Localize_Swift.xcodeproj/project.pbxproj b/Localize_Swift.xcodeproj/project.pbxproj index 4677bae..0af577c 100644 --- a/Localize_Swift.xcodeproj/project.pbxproj +++ b/Localize_Swift.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 2ED39529211CB405000723E6 /* String+LocalizeAttributed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED39528211CB405000723E6 /* String+LocalizeAttributed.swift */; }; 3433F2411C518AF7003AE34D /* Localize_Swift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3433F2361C518AF7003AE34D /* Localize_Swift.framework */; }; 3433F2461C518AF7003AE34D /* Localize_SwiftTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3433F2451C518AF7003AE34D /* Localize_SwiftTests.swift */; }; 3433F2521C518B38003AE34D /* Localize_Swift.h in Headers */ = {isa = PBXBuildFile; fileRef = 3433F2501C518B38003AE34D /* Localize_Swift.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -46,6 +47,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 2ED39528211CB405000723E6 /* String+LocalizeAttributed.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "String+LocalizeAttributed.swift"; path = "Sources/String+LocalizeAttributed.swift"; sourceTree = SOURCE_ROOT; }; 3433F2361C518AF7003AE34D /* Localize_Swift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Localize_Swift.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 3433F23B1C518AF7003AE34D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 3433F2401C518AF7003AE34D /* Localize_SwiftTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Localize_SwiftTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -131,6 +133,7 @@ 3433F2381C518AF7003AE34D /* Localize_Swift */ = { isa = PBXGroup; children = ( + 2ED39528211CB405000723E6 /* String+LocalizeAttributed.swift */, 3433F2501C518B38003AE34D /* Localize_Swift.h */, 3433F2511C518B38003AE34D /* Localize.swift */, 68A520031DA6D5FD00F43D9E /* String+LocalizeTableName.swift */, @@ -367,6 +370,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 2ED39529211CB405000723E6 /* String+LocalizeAttributed.swift in Sources */, 68973D6B1DA7AB140076F08A /* String+LocalizedBundleTableName.swift in Sources */, 68973D661DA7AA200076F08A /* String+LocalizeBundle.swift in Sources */, 3433F2531C518B38003AE34D /* Localize.swift in Sources */, @@ -470,7 +474,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -514,7 +518,7 @@ MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; diff --git a/Localize_Swift.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Localize_Swift.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Localize_Swift.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Sources/Localize.swift b/Sources/Localize.swift index 278240f..74b2c5e 100644 --- a/Sources/Localize.swift +++ b/Sources/Localize.swift @@ -131,7 +131,7 @@ open class Localize: NSObject { */ open class func defaultLanguage() -> String { var defaultLanguage: String = String() - guard let preferredLanguage = Bundle.main.preferredLocalizations.first else { + guard let preferredLanguage = Locale.current.languageCode else { return LCLDefaultLanguage } let availableLanguages: [String] = self.availableLanguages() diff --git a/Sources/String+LocalizeAttributed.swift b/Sources/String+LocalizeAttributed.swift new file mode 100644 index 0000000..036173e --- /dev/null +++ b/Sources/String+LocalizeAttributed.swift @@ -0,0 +1,117 @@ +// +// String+LocalizeAttributed.swift +// Localize_Swift +// +// Created by Smol on 10/05/2017. +// Copyright © 2017 Roy Marmelstein. All rights reserved. +// + +import Foundation +import UIKit + +// example - #[{color:3A92C4;background:00FF00}bleue] + +fileprivate extension UIColor { + convenience init(hexa: Int) { + let red : Int = hexa >> 16 & 0xFF + let green : Int = hexa >> 8 & 0xFF + let blue : Int = hexa & 0xFF + + self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0) + } +} + +public extension String { + internal func substring(from range : NSRange) -> String { + let startIndex = self.index(self.startIndex, offsetBy: range.location) + let endIndex = self.index(self.startIndex, offsetBy: range.location + range.length) + + let newRange = startIndex.. NSMutableAttributedString { + var tempMutable = mutable + let pattern : String = "\\#\\[\\{([^\\}]+)\\}(.+)\\]" + let regexp = try! NSRegularExpression(pattern: pattern, options: [.caseInsensitive]) + + + let matches : [NSTextCheckingResult] = regexp.matches(in: mutable.string, options: [], range: NSMakeRange(0, mutable.string.characters.count)) + + for match in matches { + let group = match.range(at: 0) + let styles = match.range(at: 1) + let text = match.range(at: 2) + + let temp = parse(style: mutable.string.substring(from: styles)) + + tempMutable.replaceCharacters(in: group, with: mutable.string.substring(from: text)) + + tempMutable.addAttributes(temp, range: NSMakeRange(group.location, text.length)) + tempMutable = self.parse(mutable: tempMutable) + + } + + return tempMutable + } + + private func transform() -> NSMutableAttributedString { + var mutable : NSMutableAttributedString = NSMutableAttributedString(string: self, attributes: nil) + + mutable.beginEditing() + + mutable = self.parse(mutable: mutable) + + mutable.endEditing() + return mutable + } + + func parse(style: String) -> [NSAttributedStringKey:Any]{ + var attributes : [NSAttributedStringKey:Any] = [:] + let pattern : String = "[;]?([^;:]+):([^;:}]+)" + + let regexp = try! NSRegularExpression(pattern: pattern, options: [.caseInsensitive]) + + let matches : [NSTextCheckingResult] = regexp.matches(in: style, options: [], range: NSMakeRange(0, style.characters.count)) + + for match in matches { + let key = style.substring(from: match.range(at: 1)) + let value = style.substring(from: match.range(at: 2)) + + + let temp = self.attribute(key: key, value: value) + attributes[temp.key] = temp.value + } + + return attributes + } + + func attribute(key: String, value: String) -> (key: NSAttributedStringKey, value: Any) { + switch key { + case "color": + + return (key: NSAttributedStringKey.foregroundColor, value: UIColor(hexa: Int.init(value, radix: 16)!)) + case "background": + return (key: NSAttributedStringKey.backgroundColor, value: UIColor(hexa: Int.init(value, radix: 16)!)) + case "font": + let temp = value.components(separatedBy: ",") + + guard let font = UIFont(name: temp[0], size: CGFloat(Int.init(temp[1], radix: 10) ?? Int(UIFont.systemFontSize))) else { + return (key: NSAttributedStringKey.init(""), value: "") + } + + return (key: NSAttributedStringKey.font, value: font) + case "align": + let paragraphStyle : NSMutableParagraphStyle = NSMutableParagraphStyle() + + paragraphStyle.alignment = .center + return (key: NSAttributedStringKey.paragraphStyle, value: paragraphStyle) + default: + return (key: NSAttributedStringKey.init(""), value: "") + } + } +}