Skip to content

Commit 4702252

Browse files
author
Robin Oster
committed
ROGoogleTranslate was in the wrong directory. Move ROGoogleTranslate into the Source folder.
1 parent 8fd6b85 commit 4702252

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

ROGoogleTranslate.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@
88

99
/* Begin PBXBuildFile section */
1010
CB46171F1DBE32740066AD96 /* ROGoogleTranslate.podspec in Resources */ = {isa = PBXBuildFile; fileRef = CB46171E1DBE32740066AD96 /* ROGoogleTranslate.podspec */; };
11+
CB4617211DBE36780066AD96 /* ROGoogleTranslate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB4617201DBE36780066AD96 /* ROGoogleTranslate.swift */; };
1112
CBCA3FBD1DB90B850025D8E4 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBCA3FBC1DB90B850025D8E4 /* AppDelegate.swift */; };
1213
CBCA3FBF1DB90B850025D8E4 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBCA3FBE1DB90B850025D8E4 /* ViewController.swift */; };
1314
CBCA3FC21DB90B850025D8E4 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CBCA3FC01DB90B850025D8E4 /* Main.storyboard */; };
1415
CBCA3FC41DB90B850025D8E4 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = CBCA3FC31DB90B850025D8E4 /* Assets.xcassets */; };
1516
CBCA3FC71DB90B850025D8E4 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CBCA3FC51DB90B850025D8E4 /* LaunchScreen.storyboard */; };
16-
CBCA3FD21DB90F970025D8E4 /* ROGoogleTranslate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBCA3FD11DB90F970025D8E4 /* ROGoogleTranslate.swift */; };
1717
/* End PBXBuildFile section */
1818

1919
/* Begin PBXFileReference section */
2020
CB46171E1DBE32740066AD96 /* ROGoogleTranslate.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = ROGoogleTranslate.podspec; sourceTree = SOURCE_ROOT; };
21+
CB4617201DBE36780066AD96 /* ROGoogleTranslate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ROGoogleTranslate.swift; path = Source/ROGoogleTranslate.swift; sourceTree = SOURCE_ROOT; };
2122
CBCA3FB91DB90B850025D8E4 /* ROGoogleTranslate.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ROGoogleTranslate.app; sourceTree = BUILT_PRODUCTS_DIR; };
2223
CBCA3FBC1DB90B850025D8E4 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
2324
CBCA3FBE1DB90B850025D8E4 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
2425
CBCA3FC11DB90B850025D8E4 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
2526
CBCA3FC31DB90B850025D8E4 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
2627
CBCA3FC61DB90B850025D8E4 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
2728
CBCA3FC81DB90B850025D8E4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
28-
CBCA3FD11DB90F970025D8E4 /* ROGoogleTranslate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ROGoogleTranslate.swift; sourceTree = "<group>"; };
2929
/* End PBXFileReference section */
3030

3131
/* Begin PBXFrameworksBuildPhase section */
@@ -81,7 +81,7 @@
8181
CBCA3FCE1DB90B8C0025D8E4 /* Source */ = {
8282
isa = PBXGroup;
8383
children = (
84-
CBCA3FD11DB90F970025D8E4 /* ROGoogleTranslate.swift */,
84+
CB4617201DBE36780066AD96 /* ROGoogleTranslate.swift */,
8585
);
8686
name = Source;
8787
sourceTree = "<group>";
@@ -161,7 +161,7 @@
161161
buildActionMask = 2147483647;
162162
files = (
163163
CBCA3FBF1DB90B850025D8E4 /* ViewController.swift in Sources */,
164-
CBCA3FD21DB90F970025D8E4 /* ROGoogleTranslate.swift in Sources */,
164+
CB4617211DBE36780066AD96 /* ROGoogleTranslate.swift in Sources */,
165165
CBCA3FBD1DB90B850025D8E4 /* AppDelegate.swift in Sources */,
166166
);
167167
runOnlyForDeploymentPostprocessing = 0;

ROGoogleTranslate/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>APPL</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>0.0.2</string>
18+
<string>0.0.3</string>
1919
<key>CFBundleVersion</key>
2020
<string>16298</string>
2121
<key>LSRequiresIPhoneOS</key>

Source/ROGoogleTranslate.swift

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// ROGoogleTranslate.swift
3+
// ROGoogleTranslate
4+
//
5+
// Created by Robin Oster on 20/10/16.
6+
// Copyright © 2016 prine.ch. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public struct ROGoogleTranslateParams {
12+
var source = "de"
13+
var target = "en"
14+
var text = "Ich glaube, du hast den Text zum Übersetzen vergessen"
15+
}
16+
17+
18+
/// Offers easier access to the Google Translate API
19+
open class ROGoogleTranslate {
20+
21+
/// Store here the Google Translate API Key
22+
public var apiKey = ""
23+
24+
///
25+
/// Initial constructor
26+
///
27+
public init() {
28+
29+
}
30+
31+
///
32+
/// Translate a phrase from one language into another
33+
///
34+
/// - parameter params: ROGoogleTranslate Struct contains all the needed parameters to translate with the Google Translate API
35+
/// - parameter callback: The translated string will be returned in the callback
36+
///
37+
open func translate(params:ROGoogleTranslateParams, callback:@escaping (_ translatedText:String) -> ()) {
38+
39+
guard apiKey != "" else {
40+
print("Warning: You should set the api key before calling the translate method.")
41+
return
42+
}
43+
44+
if let urlEncodedText = params.text.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) {
45+
if let url = URL(string: "https://www.googleapis.com/language/translate/v2?key=\(self.apiKey)&q=\(urlEncodedText)&source=\(params.source)&target=\(params.target)") {
46+
47+
let httprequest = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
48+
guard error == nil else {
49+
print("Something went wrong: \(error?.localizedDescription)")
50+
return
51+
}
52+
53+
if let httpResponse = response as? HTTPURLResponse {
54+
55+
guard httpResponse.statusCode == 200 else {
56+
print("Something went wrong: \(data)")
57+
return
58+
}
59+
60+
do {
61+
// Pyramid of optional json retrieving. I know with SwiftyJSON it would be easier, but I didn't want to add an external library
62+
if let data = data {
63+
if let json = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {
64+
if let jsonData = json["data"] as? [String : Any] {
65+
if let translations = jsonData["translations"] as? [NSDictionary] {
66+
if let translation = translations.first as? [String : Any] {
67+
if let translatedText = translation["translatedText"] as? String {
68+
callback(translatedText)
69+
}
70+
}
71+
}
72+
}
73+
}
74+
}
75+
} catch {
76+
print("Serialization failed: \(error.localizedDescription)")
77+
}
78+
}
79+
})
80+
81+
httprequest.resume()
82+
}
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)