Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
✨ adding a way to localize Storyboards
  • Loading branch information
igorkulman committed Oct 24, 2019
commit 97c11f9580a693d40f8b58f6af1b932b3394c874
8 changes: 8 additions & 0 deletions sources/LocalizationEditor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
F3960617223A4084006C7B16 /* AddViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3960616223A4084006C7B16 /* AddViewController.swift */; };
F396061E223A5B5A006C7B16 /* LocalizationProviderAddingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F396061D223A5B5A006C7B16 /* LocalizationProviderAddingTests.swift */; };
F3A5ABB8220448690077460D /* FileManager+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3A5ABB7220448690077460D /* FileManager+Extension.swift */; };
F3AD2CD4236190F400558765 /* NSView+Localization.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3AD2CD3236190F400558765 /* NSView+Localization.swift */; };
F3AD2CD62361935100558765 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = F3AD2CD52361935100558765 /* Localizable.strings */; };
F3BF7E6A223A8AE10043F2CB /* Special.strings in Resources */ = {isa = PBXBuildFile; fileRef = F3BF7E69223A8AE10043F2CB /* Special.strings */; };
F3C4FA7D222FBB9B00280EF4 /* LocalizationProviderDeletingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3C4FA7C222FBB9B00280EF4 /* LocalizationProviderDeletingTests.swift */; };
F3C5BF252299529D001BC8FB /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = F3C5BF242299529D001BC8FB /* Credits.rtf */; };
Expand Down Expand Up @@ -93,6 +95,8 @@
F3960616223A4084006C7B16 /* AddViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddViewController.swift; sourceTree = "<group>"; };
F396061D223A5B5A006C7B16 /* LocalizationProviderAddingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalizationProviderAddingTests.swift; sourceTree = "<group>"; };
F3A5ABB7220448690077460D /* FileManager+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FileManager+Extension.swift"; sourceTree = "<group>"; };
F3AD2CD3236190F400558765 /* NSView+Localization.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSView+Localization.swift"; sourceTree = "<group>"; };
F3AD2CD52361935100558765 /* Localizable.strings */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; path = Localizable.strings; sourceTree = "<group>"; };
F3BF7E69223A8AE10043F2CB /* Special.strings */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; path = Special.strings; sourceTree = "<group>"; };
F3C4FA7C222FBB9B00280EF4 /* LocalizationProviderDeletingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalizationProviderDeletingTests.swift; sourceTree = "<group>"; };
F3C5BF242299529D001BC8FB /* Credits.rtf */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; path = Credits.rtf; sourceTree = "<group>"; };
Expand Down Expand Up @@ -175,6 +179,7 @@
F3D9C94E20BEC7460081830A /* Info.plist */,
F3D9C94F20BEC7460081830A /* LocalizationEditor.entitlements */,
F3C5BF242299529D001BC8FB /* Credits.rtf */,
F3AD2CD52361935100558765 /* Localizable.strings */,
);
path = LocalizationEditor;
sourceTree = "<group>";
Expand Down Expand Up @@ -278,6 +283,7 @@
children = (
F3A5ABB7220448690077460D /* FileManager+Extension.swift */,
F3EF22062209D650000E8C80 /* String+Extensions.swift */,
F3AD2CD3236190F400558765 /* NSView+Localization.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -374,6 +380,7 @@
F3D9C96320BEDDE20081830A /* LocalizationCell.xib in Resources */,
F3C5BF252299529D001BC8FB /* Credits.rtf in Resources */,
F3D9C96520BEDF580081830A /* KeyCell.xib in Resources */,
F3AD2CD62361935100558765 /* Localizable.strings in Resources */,
F3D9C94A20BEC7460081830A /* Assets.xcassets in Resources */,
F3261F66222EA9BF006838C3 /* ActionsCell.xib in Resources */,
F3D9C94D20BEC7460081830A /* Main.storyboard in Resources */,
Expand Down Expand Up @@ -447,6 +454,7 @@
F3D9C96720BEDF640081830A /* KeyCell.swift in Sources */,
A15A5A4221D908C400D3C791 /* Parser.swift in Sources */,
F3A5ABB8220448690077460D /* FileManager+Extension.swift in Sources */,
F3AD2CD4236190F400558765 /* NSView+Localization.swift in Sources */,
F3D9C95720BEC9F10081830A /* LocalizationString.swift in Sources */,
172274D420D933970036EA4E /* LocalizationGroup.swift in Sources */,
F3D9C95C20BECDBF0081830A /* LocalizationProvider.swift in Sources */,
Expand Down
193 changes: 193 additions & 0 deletions sources/LocalizationEditor/Extensions/NSView+Localization.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
//
// NSVIew+Localization.swift
// LocalizationEditor
//
// Created by Igor Kulman on 24/10/2019.
// Copyright © 2019 Igor Kulman. All rights reserved.
//
// Inspired by https://github.com/PiXeL16/IBLocalizable for iOS

import AppKit
import Foundation

/**
* Localizable Protocol
*/
protocol Localizable: AnyObject {
/// The property that can be localized for each view, for example in a UILabel its the text, in a UIButton its the title, etc
var localizableProperty: String? { get set }

/// The localizable string value in the your localizable strings
var localizableString: String { get set }

/**
Applies the localizable string to the supported view attribute
*/
func applyLocalizableString(_ localizableString: String?)
}

extension Localizable {
/**
Applies the localizable string to the supported view attribute

- parameter localizableString: localizable String Value
*/
public func applyLocalizableString(_ localizableString: String?) {
localizableProperty = localizableString?.localized
}
}

extension NSCell: Localizable {
/// Not implemented in base class
@objc var localizableProperty: String? {
get {
return ""
}
set {}
}

/// Applies the localizable string to the localizable field of the supported view
@IBInspectable var localizableString: String {
get {
guard let text = self.localizableProperty else {
return ""
}
return text
}
set {
/**
* Applys the localization to the property
*/
applyLocalizableString(newValue)
}
}
}

extension NSMenuItem: Localizable {
/// Not implemented in base class
@objc var localizableProperty: String? {
get {
return title
}
set {
title = newValue ?? ""
}
}

/// Applies the localizable string to the localizable field of the supported view
@IBInspectable var localizableString: String {
get {
guard let text = self.localizableProperty else {
return ""
}
return text
}
set {
/**
* Applys the localization to the property
*/
applyLocalizableString(newValue)
}
}

func applyLocalizableString(_ localizableString: String?) {
title = localizableString?.localized ?? ""
}
}

extension NSMenu {
/// Not implemented in base class
@objc var localizableProperty: String? {
get {
return title
}
set {
title = newValue ?? ""
}
}

/// Applies the localizable string to the localizable field of the supported view
@IBInspectable var localizableString: String {
get {
guard let text = self.localizableProperty else {
return ""
}
return text
}
set {
/**
* Applys the localization to the property
*/
applyLocalizableString(newValue)
}
}

func applyLocalizableString(_ localizableString: String?) {
title = localizableString?.localized ?? ""
}
}

extension NSSearchField {
/// Not implemented in base class
@objc var localizableProperty: String? {
get {
return placeholderString
}
set {
placeholderString = newValue ?? ""
}
}

/// Applies the localizable string to the localizable field of the supported view
@IBInspectable var localizableString: String {
get {
guard let text = self.localizableProperty else {
return ""
}
return text
}
set {
/**
* Applys the localization to the property
*/
applyLocalizableString(newValue)
}
}

func applyLocalizableString(_ localizableString: String?) {
placeholderString = localizableString?.localized ?? ""
}
}

extension NSTextFieldCell {
public override var localizableProperty: String? {
get {
return title
}
set {
title = newValue ?? ""
}
}
}

extension NSButtonCell {
public override var localizableProperty: String? {
get {
return title
}
set {
title = newValue ?? ""
}
}
}

extension NSPopUpButtonCell {
public override var localizableProperty: String? {
get {
return title
}
set {
title = newValue ?? ""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ extension String {
var escaped: String {
return self.replacingOccurrences(of: "\"", with: "\\\"").replacingOccurrences(of: "\n", with: "\\n")
}

var localized: String {
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
}
}