简体   繁体   English

如何创建一个类并返回一些东西。 迅速

[英]how to create a class and return something. Swift

I am trying to create multiple languages for my app, so I want to translate a English word to a different language word, but I am not sure how to create a class and return the translated word like Translate("hello") and it would return 你好 . 我正在尝试为我的应用创建多种语言,因此我想将英语单词翻译为其他语言单词,但是我不确定如何创建一个类并返回翻译后的单词,例如Translate("hello") ,它将回你好 Any suggestions instead of creating a function as I don't want to do something like Translate().convert("hello") and get the return of "你好"? 有什么建议而不是创建一个函数,因为我不想做类似Translate().convert("hello")并得到“你好”的回报? Thanks. 谢谢。

class Translate {
    let englishToChineseDict = [
                                "hello": "你好",
                                "hi": "你好"
                               ]
    init(english: String) {
        let word = englishToChineseDict[english]
    }
}

Since there are no properties to initialize an init method is not needed but a translate method. 由于没有用于初始化的属性,因此不需要init方法,但需要使用translate方法。

class Translate {
    let englishToChineseDict = [
                                "hello": "你好",
                                "hi": "你好"
                               ]
    func translate(english : String) -> String {

      if let result = englishToChineseDict[english] {
         return result
      } else {
         return "The term \(english) is not in the dictionary"
      }
    }
}

And use it by creating an instance of the class and call the method 并通过创建类的实例并调用方法来使用它

let translator = Translate()
let chinese = translator.translate("hi")

Alternatively use a static class 或者使用静态类

static let englishToChineseDict = [ ...
...
class func translate( ...

and call it 叫它

let chinese = Translate.translate("hi")

Edit : 编辑

Swift classes don't return anything (with the exception nil in a failable initializer) but as a workaround you could implement the CustomStringConvertible protocol which changes the class pointer to a string representation. Swift类不返回任何内容(在失败的初始化程序中除外nil ),但是,作为一种解决方法,您可以实现CustomStringConvertible协议,该协议将类指针更改为字符串表示形式。

class Translate : CustomStringConvertible {
  let englishToChineseDict = [ "hello": "你好", "hi": "你好" ]

  var english : String

  init(_ english : String) {
    self.english = english
  }

  var description : String {

    if let result = englishToChineseDict[english] {
      return result
    } else {
      return "The term \(english) is not in the dictionary"
    }
  }
}


let chinese = Translate("hi")
print(chinese) // "你好"

init method is used to initialize the property of object, try to create another function and use that like this init方法用于初始化对象的属性,尝试创建另一个函数并像这样使用

class Translate: NSObject {

    struct Static {
        static var instance: Translate?
    }

    class func sharedManager() -> Translate {
        if (Static.instance == nil)
        {
            Static.instance = Translate()
        }
        return Static.instance!
    }

    let englishToChineseDict = [
        "hello": "你好",
        "hi": "你好"
    ]

    func translateWord(english: String) -> String {
        if let word = englishToChineseDict[english] {
            return word
        }
        else { 
            return "Chinese word not found for english word \(english)"
        }
    }
}

Now call this method like this 现在像这样调用此方法

let str = Translate.sharedManager().translateWord("hello")

Note: I have created shared (singleton) object of the Translate class, so you can access singleton object from Any class using sharedManager method. 注意:我已经创建了Translate类的共享(单例)对象,因此您可以使用sharedManager方法从Any类访问单例对象。 So you no need create new instance of of Translate class. 因此,您无需创建Translate类的新实例。

Edit: 编辑:

class Translate: NSObject {

    let englishToChineseDict = [
        "hello": "你好",
        "hi": "你好"
    ]

    var translatedWord = String()
    var isTranslated = false

    init(english: String) -> String {
        if let word = englishToChineseDict[english] {
            translatedWord = word
            isTranslated = true
        }
    }
}

Now you can call like this 现在您可以像这样打电话

let translate = Translate("Hello")
if translate.isTranslate {
    print(translate.translatedWord)
}

To make your app support multiple languages, first you need to add the localization in the project settings: 为了使您的应用程序支持多种语言,首先需要在项目设置中添加本地化:

Select your project file on the left pane, then add a localization in the "Localizations" section: 在左窗格中选择项目文件,然后在“本地化”部分中添加本地化:

在此处输入图片说明

Click the "+" button and select Chinese (Traditional), check all the checkboxes: 点击“ +”按钮,然后选择中文(繁体),选中所有复选框:

在此处输入图片说明

I recommend you to also localize to Simplified Chinese. 我建议您还本地化为简体中文。 So after checking those checkboxes, the thing should look like this: 因此,在选中了这些复选框之后,该内容应如下所示:

在此处输入图片说明

You will see that some new .strings files are added to the project: 您会看到一些新的.strings文件已添加到项目中:

在此处输入图片说明

You can localize your app just by editing these files. 您只需编辑这些文件即可本地化您的应用程序。 In the files, you will see something like this: 在文件中,您将看到以下内容:

"uel-mS-zq9.text" = "some text";

You basically translate the right hand side of the = sign to the desired language. 您基本上可以将=符号的右侧翻译为所需的语言。 Simple as this: 就这么简单:

"uel-mS-zq9.text" = "一些文字";

And you do this for all the text that needs translating. 然后对所有需要翻译的文本执行此操作。

But sometimes, some text that needs translations is not in the storyboard. 但有时,某些需要翻译的文本不在情节提要中。 To translate those, you need to create a new file called Localizable.strings and check these checkboxes on the file inspector: 要翻译这些文件,您需要创建一个名为Localizable.strings的新文件,并在文件检查器中选中以下复选框:

在此处输入图片说明

Again, you'll find a Localizable.strings file created for each of the localizations. 同样,您将找到为每个Localizable.strings创建的Localizable.strings文件。

If you write something like: 如果您编写如下内容:

"Settings" = "設定";

in the Traditional Chinese file, calling NSLocalizedString("Settings", comment: "") will return 設定. 在繁体中文文件中,调用NSLocalizedString("Settings", comment: "")将返回设置。

For more info, you can check this or this out. 欲了解更多信息,可以查询这个出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM