Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rocxteady committed Oct 1, 2023
0 parents commit 6521e81
Show file tree
Hide file tree
Showing 11 changed files with 417 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
32 changes: 32 additions & 0 deletions .swiftpm/WPSwift.xctestplan
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"configurations" : [
{
"id" : "779FA71B-8A24-4208-8B95-11435B194ED1",
"name" : "Test Scheme Action",
"options" : {

}
}
],
"defaultOptions" : {
"codeCoverage" : {
"targets" : [
{
"containerPath" : "container:",
"identifier" : "WPSwift",
"name" : "WPSwift"
}
]
}
},
"testTargets" : [
{
"target" : {
"containerPath" : "container:",
"identifier" : "WPSwiftTests",
"name" : "WPSwiftTests"
}
}
],
"version" : 1
}
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>
97 changes: 97 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/WPSwift.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WPSwift"
BuildableName = "WPSwift"
BlueprintName = "WPSwift"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WPSwiftTests"
BuildableName = "WPSwiftTests"
BlueprintName = "WPSwiftTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<TestPlans>
<TestPlanReference
reference = "container:.swiftpm/WPSwift.xctestplan"
default = "YES">
</TestPlanReference>
</TestPlans>
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WPSwiftTests"
BuildableName = "WPSwiftTests"
BlueprintName = "WPSwiftTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WPSwift"
BuildableName = "WPSwift"
BlueprintName = "WPSwift"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
24 changes: 24 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "WPSwift",
defaultLocalization: "en",
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "WPSwift",
targets: ["WPSwift"]),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "WPSwift"),
.testTarget(
name: "WPSwiftTests",
dependencies: ["WPSwift"]),
]
)
66 changes: 66 additions & 0 deletions Sources/WPSwift/Localizable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// File.swift
//
//
// Created by Ulaş Sancak on 1.10.2023.
//

import Foundation

protocol AllKeys {
static var allKeys: [String] { get }
}

protocol Localizable: RawRepresentable, CaseIterable, AllKeys where RawValue == String {}

extension Localizable {
static var allKeys: [String] {
allCases.map { $0.rawValue }
}
}

public enum Localization: String, Localizable {
//Default cases
case ok
case yes
case no
case cancel
case success
case failure
case unknown
case unknownError = "unknown_error"
case error
case done
case next

//All keys as nested. This needs to be updated if another nested enum is added.
public static var allKeys: [String] {
allCases.map({ $0.rawValue })
+ Configuration.allKeys
}

//Cases for specific parts

enum Configuration {
enum Error: String, Localizable {
case notSetup = "configuration.error.notSetup"
case route = "configuration.error.route"
case namespace = "configuration.error.namespace"
}

static var allKeys: [String] {
Error.allKeys
}
}
}

extension Localizable {
var localized: String {
NSLocalizedString(rawValue, bundle: Bundle.module, comment: "")
}

func localized(_ args: CVarArg...) -> String {
String(format: localized, args)
}
}

25 changes: 25 additions & 0 deletions Sources/WPSwift/Resources/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Localizable.strings


Created by Ulaş Sancak on 1.10.2023.

*/

//General
"ok" = "OK";
"yes" = "Yes";
"no" = "No";
"cancel" = "Cancel";
"success" = "Success";
"failure" = "Failure";
"unknown" = "Unknown";
"unknown_error" = "Unknown error";
"error" = "Error";
"done" = "Done";
"next" = "Next";

//Setup Configuration
"configuration.error.notSetup" = "Wordpress Configuration was not setup. Use WPSwift.configuration function to set it up.";
"configuration.error.route" = "Wordpress `route` is empty. Use WPSwift.configuration function to set it up.";
"configuration.error.namespace" = "Wordpress `namespace` is empty. Use WPSwift.configuration function to set it up.";
25 changes: 25 additions & 0 deletions Sources/WPSwift/Resources/tr.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Localizable.strings


Created by Ulaş Sancak on 1.10.2023.

*/

//Genel
"ok" = "TAMAM";
"yes" = "Evet";
"no" = "Hayır";
"cancel" = "İptal";
"success" = "Başarılı";
"failure" = "Hata";
"unknown" = "Bilinmeyen";
"unknown_error" = "Bilinmeyen hata";
"error" = "Hata";
"done" = "Bitti";
"next" = "Sonraki";

//Setup Configuration
"configuration.error.notSetup" = "Wordpress konfigurasyonu oluşturulmamış. WPSwift.initialize fonksiyonunu kullanın.";
"configuration.error.route" = "Wordpress `route` boş görünüyor. WPSwift.initialize fonksiyonunu kullanın.";
"configuration.error.namespace" = "Wordpress `namespace` boş görünüyor. WPSwift.initialize fonksiyonunu kullanın.";
47 changes: 47 additions & 0 deletions Sources/WPSwift/WPSwift.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// The Swift Programming Language
// https://docs.swift.org/swift-book
import Foundation

enum WPConfigurationError: LocalizedError {
case wasNotSetup
case route
case namespace

var errorDescription: String? {
switch self {
case .wasNotSetup:
Localization.Configuration.Error.notSetup.localized
case .route:
Localization.Configuration.Error.route.localized
case .namespace:
Localization.Configuration.Error.namespace.localized
}
}
}

struct WPConfiguration {
let route: String
let namespace: String
}

public struct WPSwift {
static internal var configuration: WPConfiguration {
get throws {
guard let _configuration else { throw WPConfigurationError.wasNotSetup }
guard !_configuration.route.isEmpty else { throw WPConfigurationError.route }
guard !_configuration.namespace.isEmpty else { throw WPConfigurationError.namespace }
return _configuration
}
}
static private var _configuration: WPConfiguration?

static func resetConfiguration() {
_configuration = nil
}
}

public extension WPSwift {
static func initialize(route: String, namespace: String) {
WPSwift._configuration = .init(route: route, namespace: namespace)
}
}
39 changes: 39 additions & 0 deletions Tests/WPSwiftTests/LocalizationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// WPSwiftSetupTests.swift
//
//
// Created by Ulaş Sancak on 30.09.2023.
//

import XCTest
@testable import WPSwift

class LocalizationTests: XCTestCase {

func testLocalizations() throws {
let localizations = Bundle.module.localizations
for localization in localizations {
guard let path = Bundle.module.path(forResource: localization, ofType: "lproj"),
let bundle = Bundle(path: path) else {
XCTAssert(false, "Localization file is missing for '" + localization + "'.")
return
}
guard let localizationPath = bundle.path(forResource: "Localizable", ofType: "strings") else {
XCTAssert(false, "Localizable.strings file is missing for " + localization + ".")
return
}
guard let localizationKeys = NSDictionary(contentsOfFile: localizationPath)?.allKeys as? [String] else {
XCTAssert(false, localization + "localization file is curropted.")
return
}
let allKeys = Localization.allKeys
for key in allKeys {
XCTAssert(localizationKeys.contains(key), key + " key is missing from the " + localization + " file")
}
for key in localizationKeys {
XCTAssert(allKeys.contains(key), "Found key '" + key + "' in localization file(" + localization + ") is not in Localization nested enum.")
}
}
}

}
Loading

0 comments on commit 6521e81

Please sign in to comment.