Skip to content

Commit

Permalink
Merge remote-tracking branch 'marmelroy/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rokoroku committed Nov 23, 2015
2 parents 9c03843 + 39d856d commit 8dda294
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ examples/LanguageSwitch/Pods/Target Support Files/Pods/Pods-umbrella.h
examples/LanguageSwitch/Pods/Target Support Files/Pods/Pods.debug.xcconfig
examples/LanguageSwitch/Pods/Target Support Files/Pods/Pods.modulemap
examples/LanguageSwitch/Pods/Target Support Files/Pods/Pods.release.xcconfig
examples/LanguageSwitch/Sample.xcworkspace/xcuserdata/marmelroy.xcuserdatad/UserInterfaceState.xcuserstate
examples/LanguageSwitch/Sample.xcworkspace/xcuserdata/marmelroy.xcuserdatad/UserInterfaceState.xcuserstate
build
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ env:
- LC_CTYPE=en_US.UTF-8
- LANG=en_US.UTF-8
before_install:
- brew update;
- brew reinstall xctool
- brew update || brew update
- brew outdated xctool || brew upgrade xctool
- gem install cocoapods
- xcrun simctl list
install: echo "<3"
Expand Down
50 changes: 50 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Change Log

## [Unreleased](https://github.com/marmelroy/Localize-Swift/tree/HEAD)

[Full Changelog](https://github.com/marmelroy/Localize-Swift/compare/0.5...HEAD)

**Merged pull requests:**

- Spellings [\#10](https://github.com/marmelroy/Localize-Swift/pull/10) ([jameshartt](https://github.com/jameshartt))

## [0.5](https://github.com/marmelroy/Localize-Swift/tree/0.5) (2015-11-03)
[Full Changelog](https://github.com/marmelroy/Localize-Swift/compare/0.4...0.5)

**Merged pull requests:**

- Much type-safer swift .localize implementation [\#9](https://github.com/marmelroy/Localize-Swift/pull/9) ([rshev](https://github.com/rshev))

## [0.4](https://github.com/marmelroy/Localize-Swift/tree/0.4) (2015-10-10)
[Full Changelog](https://github.com/marmelroy/Localize-Swift/compare/0.3...0.4)

**Closed issues:**

- Can not get localized string in swift files using genstrings.py [\#8](https://github.com/marmelroy/Localize-Swift/issues/8)
- Suggestion: Change the Localized\(\) method name to localized\(\) to match style best practices [\#7](https://github.com/marmelroy/Localize-Swift/issues/7)

## [0.3](https://github.com/marmelroy/Localize-Swift/tree/0.3) (2015-09-28)
[Full Changelog](https://github.com/marmelroy/Localize-Swift/compare/0.2...0.3)

## [0.2](https://github.com/marmelroy/Localize-Swift/tree/0.2) (2015-09-26)
[Full Changelog](https://github.com/marmelroy/Localize-Swift/compare/0.1...0.2)

**Closed issues:**

- Swift 2.0 Compatibility [\#6](https://github.com/marmelroy/Localize-Swift/issues/6)
- Add unit tests [\#5](https://github.com/marmelroy/Localize-Swift/issues/5)
- Add support for plurals with .stringsdict [\#4](https://github.com/marmelroy/Localize-Swift/issues/4)

## [0.1](https://github.com/marmelroy/Localize-Swift/tree/0.1) (2015-08-06)
[Full Changelog](https://github.com/marmelroy/Localize-Swift/compare/0.0.1...0.1)

**Closed issues:**

- Create Localize-Swift genstrings equivalent [\#3](https://github.com/marmelroy/Localize-Swift/issues/3)
- Add example code [\#2](https://github.com/marmelroy/Localize-Swift/issues/2)
- Add notification on language change [\#1](https://github.com/marmelroy/Localize-Swift/issues/1)

## [0.0.1](https://github.com/marmelroy/Localize-Swift/tree/0.0.1) (2015-08-05)


\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
4 changes: 2 additions & 2 deletions Localize-Swift.podspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Pod::Spec.new do |s|
s.name = "Localize-Swift"
s.version = "0.4"
s.version = "0.6"
s.summary = "Swift-friendly localization and i18n with in-app language switching."
s.description = "Localize-Swift is a simple framework that improves i18n and localization in Swift iOS apps - providing cleaner syntax and in-app language switching."
s.homepage = "https://github.com/marmelroy/Localize-Swift"
s.license = "MIT"
s.author = { "Roy Marmelstein" => "[email protected]" }
s.social_media_url = "http://twitter.com/marmelroy"
s.source = { :git => "https://github.com/marmelroy/Localize-Swift.git", :tag => "0.4" }
s.source = { :git => "https://github.com/marmelroy/Localize-Swift.git", :tag => "0.6" }
s.source_files = "Localize"
s.platform = :ios
s.ios.deployment_target = "8.0"
Expand Down
8 changes: 4 additions & 4 deletions Localize.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 4;
CURRENT_PROJECT_VERSION = 6;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
Expand Down Expand Up @@ -210,7 +210,7 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 4;
CURRENT_PROJECT_VERSION = 6;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
Expand Down Expand Up @@ -238,7 +238,7 @@
CLANG_ENABLE_MODULES = YES;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 4;
DYLIB_CURRENT_VERSION = 6;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = Localize/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
Expand All @@ -257,7 +257,7 @@
CLANG_ENABLE_MODULES = YES;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 4;
DYLIB_CURRENT_VERSION = 6;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = Localize/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
Expand Down
4 changes: 2 additions & 2 deletions Localize/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.4</string>
<string>0.6</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>4</string>
<string>6</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
Expand Down
78 changes: 57 additions & 21 deletions Localize/Localize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,55 @@

import Foundation

/// Internal current language key
let LCLCurrentLanguageKey : String = "LCLCurrentLanguageKey"

/// Default language. English. If English is unavailable defaults to base localization.
let LCLDefaultLanguage : String = "en"

/// Name for language change notification
public let LCLLanguageChangeNotification : String = "LCLLanguageChangeNotification"

// MARK: Localization Syntax

// Swift 1.x friendly localization syntax, replaces NSLocalizedString
/**
Swift 1.x friendly localization syntax, replaces NSLocalizedString
- Parameter string: Key to be localized.
- Returns: The localized string.
*/
public func Localized(string: String) -> String {
let path = NSBundle.mainBundle().pathForResource(Localize.currentLanguage(), ofType: "lproj")
let bundle = NSBundle(path: path!)
let string = bundle?.localizedStringForKey(string, value: nil, table: nil)
return string!
if let path = NSBundle.mainBundle().pathForResource(Localize.currentLanguage(), ofType: "lproj"), bundle = NSBundle(path: path) {
return bundle.localizedStringForKey(string, value: nil, table: nil)
}
return string
}

// Swift 1.x friendly localization syntax with format arguments, replaces String(format:NSLocalizedString)
public func Localized(string: String, args: CVarArgType...) -> String {
/**
Swift 1.x friendly localization syntax with format arguments, replaces String(format:NSLocalizedString)
- Parameter string: Key to be localized.
- Returns: The formatted localized string with arguments.
*/
public func Localized(string: String, arguments args: CVarArgType...) -> String {
return String(format: Localized(string), arguments: args)
}

public extension String {
// Swift 2 friendly localization syntax, replaces NSLocalizedString
/**
Swift 2 friendly localization syntax, replaces NSLocalizedString
- Returns: The localized string.
*/
func localized() -> String {
let path = NSBundle.mainBundle().pathForResource(Localize.currentLanguage(), ofType: "lproj")
let bundle = NSBundle(path: path!)
let string = bundle?.localizedStringForKey(self, value: nil, table: nil)
return string!
if let path = NSBundle.mainBundle().pathForResource(Localize.currentLanguage(), ofType: "lproj"), bundle = NSBundle(path: path) {
return bundle.localizedStringForKey(self, value: nil, table: nil)
}
return self
}

// Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString)
func localizedWithFormat(args: CVarArgType...) -> String {
/**
Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString)
- Returns: The formatted localized string with arguments.
*/
func localizedWithFormat(arguments args: CVarArgType...) -> String {
return String(format: localized(), arguments: args)
}
}
Expand All @@ -49,12 +67,18 @@ public extension String {

public class Localize: NSObject {

// Returns a list of available localizations
/**
List available languages
- Returns: Array of available languages.
*/
public class func availableLanguages() -> [String] {
return NSBundle.mainBundle().localizations
}

// Returns the current language
/**
Current language
- Returns: The current language. String.
*/
public class func currentLanguage() -> String {
var currentLanguage : String = String()
if ((NSUserDefaults.standardUserDefaults().objectForKey(LCLCurrentLanguageKey)) != nil){
Expand All @@ -66,7 +90,10 @@ public class Localize: NSObject {
return currentLanguage
}

// Change the current language
/**
Change the current language
- Parameter language: Desired language.
*/
public class func setCurrentLanguage(language: String) {
var selectedLanguage: String = String()
let availableLanguages : [String] = self.availableLanguages()
Expand All @@ -83,7 +110,10 @@ public class Localize: NSObject {
}
}

// Returns the app's default language
/**
Default language
- Returns: The app's default language. String.
*/
class func defaultLanguage() -> String {
var defaultLanguage : String = String()
let preferredLanguage = NSBundle.mainBundle().preferredLocalizations.first!
Expand All @@ -97,12 +127,18 @@ public class Localize: NSObject {
return defaultLanguage
}

// Resets the current language to the default
public class func resetCurrentLanaguageToDefault() {
/**
Resets the current language to the default
*/
public class func resetCurrentLanguageToDefault() {
setCurrentLanguage(self.defaultLanguage())
}

// Returns the app's full display name in the current language
/**
Get the current language's display name for a language.
- Parameter language: Desired language.
- Returns: The localized string.
*/
public class func displayNameForLanguage(language: String) -> String {
let currentLanguage : String = self.currentLanguage()
let locale : NSLocale = NSLocale(localeIdentifier: currentLanguage)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ NSNotificationCenter.defaultCenter().addObserver(self, selector: "setText", name

To reset back to the default app language:
```
Localize.resetCurrentLanaguageToDefault()
Localize.resetCurrentLanguageToDefault()
```

## genstrings
Expand Down Expand Up @@ -82,7 +82,7 @@ To integrate Localize-Swift into your Xcode project using Carthage, specify it i
github "marmelroy/Localize-Swift"
```

### Setting up with [CocoaPods](http://cocoapods.org/?q=libPhoneNumber-iOS)
### Setting up with [CocoaPods](http://cocoapods.org/?q=Localize-Swift)
```
source 'https://github.com/CocoaPods/Specs.git'
pod 'Localize-Swift', '~> 0.2'
Expand Down
2 changes: 1 addition & 1 deletion examples/LanguageSwitch/Sample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ViewController: UIViewController {
}

@IBAction func doResetLanguage(sender: AnyObject) {
Localize.resetCurrentLanaguageToDefault()
Localize.resetCurrentLanguageToDefault()
}
}

4 changes: 2 additions & 2 deletions examples/LanguageSwitch/SampleTests/SampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SampleTests: XCTestCase {

override func setUp() {
super.setUp()
Localize.resetCurrentLanaguageToDefault()
Localize.resetCurrentLanguageToDefault()
}

func testSwift1Syntax() {
Expand All @@ -36,7 +36,7 @@ class SampleTests: XCTestCase {
XCTAssertEqual(testString.localized(), "Hola mundo")
Localize.setCurrentLanguage("de")
XCTAssertEqual(testString.localized(), "Hallo Welt")
Localize.resetCurrentLanaguageToDefault()
Localize.resetCurrentLanguageToDefault()
XCTAssertEqual(testString.localized(), "Hello world")
}

Expand Down

0 comments on commit 8dda294

Please sign in to comment.