【问题标题】:Returning a processed String of an Enum on Swift iOS在 Swift iOS 上返回已处理的枚举字符串
【发布时间】:2016-03-24 11:56:13
【问题描述】:

我刚刚为我的项目创建了一个本地化模块,由于我是 Swift 新手,所以我知道以下是否可行。

我有一个这样的枚举:

enum Localizations : String
{
    case StringId1 = "string_to_translate_1"
    case StringId2 = "string_to_translate_2"
    case StringId3 = "string_to_translate_3"
    var localized : String {
        return NSLocalizedString(self.rawValue, tableName: nil, bundle: NSBundle.mainBundle(), value: "", comment: "")
    }
}

有了这个枚举,我可以用这个命令获得本地化的字符串:

let myString = Localizations.StringId1.localized

但是当您必须放置大量字符串时,.localized 就像是多余的,因为您之前已经有了本地化。 所以我正在寻找的是如果我可以做这样的事情:

let myString = Localizations.StringId1

myString 类似于“按下按钮继续”

我已经设法做了一些有效的事情,但不是在所有情况下。

在此链接中找到:https://appventure.me/2015/10/17/advanced-practical-enum-examples/ 在“高级枚举使用协议”步骤中,它建议进行如下修改会得到我想要的:

protocol CustomStringConvertible {
  var description: String { get }
}

enum Trade: CustomStringConvertible {
   case Buy, Sell
   var description: String {
       switch self {
       case Buy: return "We're buying something"
       case Sell: return "We're selling something"
       }
   }
}

let action = Trade.Buy
print("this action is \(action)")
// prints: this action is We're buying something

我的修改是这些:

protocol CustomEnumString {
    var localized: String { get }
}

enum Localizations : String, CustomEnumString
{
    case StringId1 = "string_to_translate_1"
    case StringId2 = "string_to_translate_2"
    case StringId3 = "string_to_translate_3"
    var localized : String {
        return NSLocalizedString(self.rawValue, tableName: nil, bundle: NSBundle.mainBundle(), value: "", comment: "")
    }
}

但是当打印它时,如果显示枚举字面量,并且当传递给一个函数时,编译器告诉我它是无效的:

let localizedString = Localizations.StringId1
print("localization: \(localizedString)")
// prints: "localization: StringId1"

// note: AlertStrings is an struct with two strings
// this fails to compile saying that cannot convert value of type 'Localizations' to expected argument type 'String'
let alertStrings = AlertStrings(title: Localizations.StringId1, message: Localizations.StringId2)
// this one works, but it's not the purpose I had in mind
let alertStrings = AlertStrings(title: Localizations.StringId1.localized, message: Localizations.StringId2.localized)

所以...简而言之,我希望能够做到这一点:

let localizedString = Localizations.StringId1
print("localization: \(localizedString)")
// prints: "localization: Press Button To Continue"

let alertStrings = AlertStrings(title: Localizations.StringId1, message: Localizations.StringId2)

但在枚举中,我只想指定一次文字,而不是先在 case 上,然后在 switch 内。

提前致谢!

【问题讨论】:

  • 这是一个very good article,关于 Swift 中的本地化以实现健壮的架构

标签: ios string swift enums


【解决方案1】:

phelgo,NSBarcelona 的一个成员刚刚告诉我这个,并且工作得很好!

enum Localizations {
    static let StringId1 = NSLocalizedString("string_to_translate_1", comment: "")
}

let myString = Localizations.StringId1

enum 没有案例可能看起来很陌生,但我们可以 保持其所有安全性(和代码完成),同时仍然防止 Localizations 被错误实例化(如果它是 struct)

【讨论】:

  • 为此,另一种有效的方法是使用 struct 将所有字符串作为计算变量 (static var)。对于那些可能被格式化的,然后使用静态方法。
【解决方案2】:

将您的enum 改为CustomStringConvertible 并将localized 重命名为description

enum Localizations: String, CustomStringConvertible
{
    case StringId1 = "string_to_translate_1"
    case StringId2 = "string_to_translate_2"
    case StringId3 = "string_to_translate_3"

    var description : String {
        return NSLocalizedString(self.rawValue, tableName: nil, bundle: NSBundle.mainBundle(), value: "", comment: "")
    }
}

let alertStrings = AlertStrings(title: Localizations.StringId1.description, message: Localizations.StringId2.description)

【讨论】:

  • 谢谢!但它不起作用:(我已经用这些更改修改了帖子以供其他人查看。
  • Localizations.StringId1 的类型为 Locolizations。 Swift 不会自动将其转换为 String。您可以调用 rawValuedescription 从枚举中获取 String
  • 是的,我想做的是直接调用枚举,它自己返回本地化的字符串。
猜你喜欢
  • 1970-01-01
  • 2018-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-12
  • 1970-01-01
相关资源
最近更新 更多