【问题标题】:iOS - Localize a static constantsiOS - 本地化静态常量
【发布时间】:2018-12-24 14:57:04
【问题描述】:

我在常量类中有这个静态 let:

struct Constants {
   struct AlertsIdentifiers {
     static let SERVER_RESTART_MESSAGE = """
     The camera will restart now.
     Please reconnect after two minutes.
    """
   }
  }

我开始本地化应用程序。 如何本地化这种字符串?

在我的 localize.string 文件中使用以下代码:

"The camera will restart now.Please reconnect after two minutes." = "The camera will restart now.Please reconnect after two minutes.";

【问题讨论】:

    标签: ios swift localization constants


    【解决方案1】:

    你可以试试

    static let SERVER_RESTART_MESSAGE =  NSLocalizedString("camReConnect", comment: "")
    

    "camReConnect" = "The camera will restart now. \n Please reconnect after two minutes.";
    

    在使其可读的同时缩短密钥的长度是一个很好的做法

    【讨论】:

    • 这是一个好主意,但我需要像上面的原始约定一样在警报中显示它 - 即 2 行,一个在另一个之上。做香?
    • "now.\nPlease"
    • 缺少\n!我应该看到了,谢谢!
    • 顺便说一句 - 大文本也有同样的问题:static let PENDING_DEVICE_SAVED_MESSAGE = """ 完成载入: 1. 点击保存并断开与相机的连接。2. 连接到互联网。3.登录云。4.打开待处理的相机屏幕。“””尝试了同样的方法,但没有,有什么想法吗?
    • @Rob 我同意澄清与有意义的长键齐头并进,但我的意思不是让它像操作员正在做的那样,快捷键的问题因开发人员而异并且肯定会留下 30% 的可能性用于挖掘
    【解决方案2】:

    您可以在可本地化的文件中定义字符串并在整个项目中使用它,

    Localizable.strings 文件中,

    "SERVER_RESTART_MESSAGE" = "The camera will restart now.Please reconnect after two minutes.";
    

    然后在Constants 中使用类似的字符串,

    struct Constants {
       struct AlertsIdentifiers {
         static let SERVER_RESTART_MESSAGE = NSLocalizedString("SERVER_RESTART_MESSAGE", comment: "")
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多