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

Supporting Storyboard based translations #111

Open
wants to merge 7 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
24 changes: 21 additions & 3 deletions Localize_Swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
68A520051DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68A520031DA6D5FD00F43D9E /* String+LocalizeTableName.swift */; };
68A520061DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68A520031DA6D5FD00F43D9E /* String+LocalizeTableName.swift */; };
68A520071DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68A520031DA6D5FD00F43D9E /* String+LocalizeTableName.swift */; };
A92704AD20A0A3BB00122600 /* Bundle+Localize.swift in Sources */ = {isa = PBXBuildFile; fileRef = A92704AC20A0A3BB00122600 /* Bundle+Localize.swift */; };
A93287AB2079320500CFADC0 /* NSBundle+Language.m in Sources */ = {isa = PBXBuildFile; fileRef = A93287A92079320400CFADC0 /* NSBundle+Language.m */; };
A93287AC2079320500CFADC0 /* NSBundle+Language.h in Headers */ = {isa = PBXBuildFile; fileRef = A93287AA2079320400CFADC0 /* NSBundle+Language.h */; settings = {ATTRIBUTES = (Public, ); }; };
A93287AD2079327500CFADC0 /* NSBundle+Language.h in Headers */ = {isa = PBXBuildFile; fileRef = A93287AA2079320400CFADC0 /* NSBundle+Language.h */; settings = {ATTRIBUTES = (Public, ); }; };
A93287AE2079327600CFADC0 /* NSBundle+Language.h in Headers */ = {isa = PBXBuildFile; fileRef = A93287AA2079320400CFADC0 /* NSBundle+Language.h */; settings = {ATTRIBUTES = (Public, ); }; };
A93287AF2079327600CFADC0 /* NSBundle+Language.h in Headers */ = {isa = PBXBuildFile; fileRef = A93287AA2079320400CFADC0 /* NSBundle+Language.h */; settings = {ATTRIBUTES = (Public, ); }; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -60,6 +66,9 @@
68973D651DA7AA200076F08A /* String+LocalizeBundle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "String+LocalizeBundle.swift"; path = "Sources/String+LocalizeBundle.swift"; sourceTree = SOURCE_ROOT; };
68973D6A1DA7AB140076F08A /* String+LocalizedBundleTableName.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "String+LocalizedBundleTableName.swift"; path = "Sources/String+LocalizedBundleTableName.swift"; sourceTree = SOURCE_ROOT; };
68A520031DA6D5FD00F43D9E /* String+LocalizeTableName.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.swift; name = "String+LocalizeTableName.swift"; path = "Sources/String+LocalizeTableName.swift"; sourceTree = SOURCE_ROOT; tabWidth = 4; usesTabs = 0; };
A92704AC20A0A3BB00122600 /* Bundle+Localize.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Bundle+Localize.swift"; path = "Sources/Bundle+Localize.swift"; sourceTree = SOURCE_ROOT; };
A93287A92079320400CFADC0 /* NSBundle+Language.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSBundle+Language.m"; path = "Sources/NSBundle+Language.m"; sourceTree = SOURCE_ROOT; };
A93287AA2079320400CFADC0 /* NSBundle+Language.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSBundle+Language.h"; path = "Sources/NSBundle+Language.h"; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -135,6 +144,9 @@
68A520031DA6D5FD00F43D9E /* String+LocalizeTableName.swift */,
68973D651DA7AA200076F08A /* String+LocalizeBundle.swift */,
68973D6A1DA7AB140076F08A /* String+LocalizedBundleTableName.swift */,
A92704AC20A0A3BB00122600 /* Bundle+Localize.swift */,
A93287AA2079320400CFADC0 /* NSBundle+Language.h */,
A93287A92079320400CFADC0 /* NSBundle+Language.m */,
3433F23B1C518AF7003AE34D /* Info.plist */,
344169461C67539300B93D28 /* Foundation.framework */,
);
Expand All @@ -158,6 +170,7 @@
buildActionMask = 2147483647;
files = (
3433F2521C518B38003AE34D /* Localize_Swift.h in Headers */,
A93287AC2079320500CFADC0 /* NSBundle+Language.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -166,6 +179,7 @@
buildActionMask = 2147483647;
files = (
343A6DD61D1529560081AA37 /* Localize_Swift.h in Headers */,
A93287AD2079327500CFADC0 /* NSBundle+Language.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -174,6 +188,7 @@
buildActionMask = 2147483647;
files = (
343A6DE31D152B1B0081AA37 /* Localize_Swift.h in Headers */,
A93287AE2079327600CFADC0 /* NSBundle+Language.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -182,6 +197,7 @@
buildActionMask = 2147483647;
files = (
343A6DF01D152E5A0081AA37 /* Localize_Swift.h in Headers */,
A93287AF2079327600CFADC0 /* NSBundle+Language.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -290,7 +306,7 @@
TargetAttributes = {
3433F2351C518AF7003AE34D = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0900;
LastSwiftMigration = 0920;
};
3433F23F1C518AF7003AE34D = {
CreatedOnToolsVersion = 7.2;
Expand Down Expand Up @@ -366,6 +382,8 @@
buildActionMask = 2147483647;
files = (
68973D6B1DA7AB140076F08A /* String+LocalizedBundleTableName.swift in Sources */,
A93287AB2079320500CFADC0 /* NSBundle+Language.m in Sources */,
A92704AD20A0A3BB00122600 /* Bundle+Localize.swift in Sources */,
68973D661DA7AA200076F08A /* String+LocalizeBundle.swift in Sources */,
3433F2531C518B38003AE34D /* Localize.swift in Sources */,
68A520041DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */,
Expand Down Expand Up @@ -535,7 +553,7 @@
3433F24B1C518AF7003AE34D /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
APPLICATION_EXTENSION_API_ONLY = NO;
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
Expand All @@ -558,7 +576,7 @@
3433F24C1C518AF7003AE34D /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
APPLICATION_EXTENSION_API_ONLY = NO;
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
Expand Down
95 changes: 86 additions & 9 deletions Sources/Localize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
//

import Foundation
import UIKit

/// Internal current language key
let LCLCurrentLanguageKey = "LCLCurrentLanguageKey"
let AppleLanguagesKey = "AppleLanguages"

/// Default language. English. If English is unavailable defaults to base localization.
let LCLDefaultLanguage = "en"
Expand Down Expand Up @@ -116,12 +118,29 @@ open class Localize: NSObject {
Change the current language
- Parameter language: Desired language.
*/
open class func setCurrentLanguage(_ language: String) {
open class func setCurrentLanguage(_ language: String, forcingRTL flag: Bool = false) {
let selectedLanguage = availableLanguages().contains(language) ? language : defaultLanguage()
if (selectedLanguage != currentLanguage()){
UserDefaults.standard.set(selectedLanguage, forKey: LCLCurrentLanguageKey)
UserDefaults.standard.synchronize()
NotificationCenter.default.post(name: Notification.Name(rawValue: LCLLanguageChangeNotification), object: nil)
UserDefaults.standard.set(selectedLanguage, forKey: LCLCurrentLanguageKey)
UserDefaults.standard.set([selectedLanguage], forKey: AppleLanguagesKey)
UserDefaults.standard.synchronize()
NotificationCenter.default.post(name: Notification.Name(rawValue: LCLLanguageChangeNotification),
object: nil)
Bundle.setLanguage(language, forcingRTL: flag)
}

/**
Change the current language and Restart from the Root View Controller
- Parameter language: Desired language.
*/
open class func setCurrentLanguage(_ language: String, forcingRTL flag: Bool = false, restartingFromRoot rootViewController: UIViewController, animated: Bool = false) {
self.setCurrentLanguage(language, forcingRTL: flag)
UIApplication.shared.keyWindow?.rootViewController = rootViewController

if animated {
let mainwindow = (UIApplication.shared.delegate?.window!)!
mainwindow.backgroundColor = UIColor(hue: 0.6477, saturation: 0.6314, brightness: 0.6077, alpha: 0.8)
UIView.transition(with: mainwindow, duration: 0.5, options: .transitionFlipFromLeft,
animations: nil, completion: nil)
}
}

Expand All @@ -131,7 +150,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 All @@ -147,8 +166,8 @@ open class Localize: NSObject {
/**
Resets the current language to the default
*/
open class func resetCurrentLanguageToDefault() {
setCurrentLanguage(self.defaultLanguage())
open class func resetCurrentLanguageToDefault(forcingRTL flag: Bool = false) {
self.setCurrentLanguage(self.defaultLanguage(), forcingRTL: flag)
}

/**
Expand All @@ -157,11 +176,69 @@ open class Localize: NSObject {
- Returns: The localized string.
*/
open class func displayNameForLanguage(_ language: String) -> String {
let locale : NSLocale = NSLocale(localeIdentifier: currentLanguage())
let locale : NSLocale = NSLocale(localeIdentifier: self.currentLanguage())
if let displayName = locale.displayName(forKey: NSLocale.Key.identifier, value: language) {
return displayName
}
return String()
}
}


extension UILabel {

var originalAligment: NSTextAlignment {
get {
switch self.tag {
case 989796:
return .center
case 232425:
return .justified
case 757677:
return .left
case 343332:
return .right
case 616365:
return .natural
default:
return .justified
}
}
set {
switch newValue {
case .center:
self.tag = 989796
case .justified:
self.tag = 232425
case .left:
self.tag = 757677
case .right:
self.tag = 343332
case .natural:
self.tag = 616365
}
}
}

open override func awakeFromNib() {
super.awakeFromNib()

self.originalAligment = self.textAlignment
}

open override func layoutSubviews() {
super.layoutSubviews()
if self.isKind(of: NSClassFromString("UITextFieldLabel")!) {
return // handle special case with uitextfields
}
if Localize.currentLanguage() == "ar" {
if self.originalAligment != .center {
self.textAlignment = self.originalAligment == .right ? .left : .right
}
} else {
if self.originalAligment != .center || self.originalAligment != .justified {
self.textAlignment = self.originalAligment
}
}
}
}
3 changes: 2 additions & 1 deletion Sources/Localize_Swift.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
// Copyright © 2016 Roy Marmelstein. All rights reserved.
//

@import Foundation;
#import <Foundation/Foundation.h>
#import "NSBundle+Language.h"

//! Project version number for Localize_Swift.
FOUNDATION_EXPORT double Localize_SwiftVersionNumber;
Expand Down
15 changes: 15 additions & 0 deletions Sources/NSBundle+Language.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// NSBundle+Language.h
// Localize
//
// Created by Mario Villamizar on 04/07/2018.
// Copyright © 2018 Mario Villamizar. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSBundle (Language)

+ (void)setLanguage:(NSString *)language forcingRTL:(BOOL) flag;

@end
61 changes: 61 additions & 0 deletions Sources/NSBundle+Language.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// NSBundle+Language.m
// Localize
//
// Created by Mario Villamizar on 04/07/2018.
// Copyright © 2018 Mario Villamizar. All rights reserved.
//

#import "NSBundle+Language.h"
#import <UIKit/UIKit.h>
#import <objc/runtime.h>

static const char kBundleKey = 0;

@interface BundleEx : NSBundle

@end

@implementation BundleEx

- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
NSBundle *bundle = objc_getAssociatedObject(self, &kBundleKey);
if (bundle) {
return [bundle localizedStringForKey:key value:value table:tableName];
}
else {
return [super localizedStringForKey:key value:value table:tableName];
}
}

@end

@implementation NSBundle (Language)

+ (void)setLanguage:(NSString *)language forcingRTL:(BOOL) flag
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
object_setClass([NSBundle mainBundle], [BundleEx class]);
});

if (flag == true) {
if (@available(iOS 9.0, *)) {
[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
[[UITableView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
[[UINavigationBar appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
}
} else {
if (@available(iOS 9.0, *)) {
[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
[[UITableView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
[[UINavigationBar appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
}
}

id value = language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil;
objc_setAssociatedObject([NSBundle mainBundle], &kBundleKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
4 changes: 3 additions & 1 deletion examples/LanguageSwitch/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ target 'Sample' do
use_frameworks!

# Pods for Sample
pod 'Localize-Swift', :path => '../..'
#pod 'Localize-Swift', :path => '../..'

pod 'Localize-Swift', :git => 'https://github.com/mariovillamizar/Localize-Swift.git', :commit => '554d61ff21dc0d7b14c63908cd70a00fd90d380d'

target 'SampleTests' do
inherit! :search_paths
Expand Down
Loading