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

fix to get the system's language by default and not the language bundle #73

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Localize-Swift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions Localize_Swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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, ); }; };
Expand Down Expand Up @@ -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 = "<group>"; };
3433F2401C518AF7003AE34D /* Localize_SwiftTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Localize_SwiftTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
2 changes: 1 addition & 1 deletion Sources/Localize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
117 changes: 117 additions & 0 deletions Sources/String+LocalizeAttributed.swift
Original file line number Diff line number Diff line change
@@ -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..<endIndex
return self.substring(with: newRange)
}

public var attributed : NSMutableAttributedString {
return self.transform()
}

private func parse(mutable: NSMutableAttributedString) -> 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: "")
}
}
}