【问题标题】:Possible to use variables and/or parameters with NSLocalizedString?可以在 NSLocalizedString 中使用变量和/或参数吗?
【发布时间】:2011-03-27 15:09:45
【问题描述】:

我尝试使用变量作为 NSLocalizedString 的输入参数,但我得到的只是输入参数。我究竟做错了什么?是否可以使用可变字符串值作为 NSLocalized 字符串的索引?

例如,我有一些字符串希望显示本地化版本。但是,我想使用一个变量作为 NSLocalizedString 的参数,而不是一个常量字符串。同样,我想在 NSLocalizedString 的参数中包含格式化元素,这样我就可以使用相同的格式化参数检索字符串的本地化版本。我可以执行以下操作吗:

案例 1:变量 NSLocalizedstring:

NSString *varStr = @"Index1";
NSString *string1 = NSLocalizedString(varStr,@"");

案例 2:格式化的 NSLocalizedString:

NSString *string1 = [NSString stringWithFormat:NSLocalizedString(@"This is an %@",@""),@"Apple"];

(请注意,变量可以包含任何内容,而不仅仅是一组固定的字符串。)

谢谢!

【问题讨论】:

    标签: objective-c localization nslocalizedstring


    【解决方案1】:

    如果您想要返回“This is an Apple/Orange/whatever”的本地化版本,您会想要:

    NSString *localizedVersion = NSLocalizedString(([NSString stringWithFormat:@"This is an %@", @"Apple"]), nil);
    

    (即NSLocalizedString()[NSString stringWithFormat:]的嵌套是相反的。)

    如果您想要的是要本地化的 格式,而不是替换的值,请执行以下操作:

    NSString *finalString = [NSString stringWithFormat:NSLocalizedString(@"SomeFormat", nil), @"Apple"];
    

    在你的Localizable.strings:

    SomeFormat = "This is an %@";
    

    【讨论】:

    • 是的,但变量可以包含任何内容,而不仅仅是“苹果”或“橙子”。所以我需要保持灵活性。
    • 让我附上我的答案以防万一。
    • 非常感谢;附加的答案实际上是我正在寻找的 - 我希望你很快得到一个“接受”复选框。
    • 您没有解决案例 1:“将变量和/或参数与 NSLocalizedString 一起使用?”。你不能这样做,应该只使用 const 字符串,因为本地化字符串应该在运行时之前翻译,并且字符串变量在运行时可以是任何东西。
    【解决方案2】:

    你的想法应该行得通。但是,如果您要取回输入参数,则意味着在 Localizable.strings 文件中找不到输入参数作为键。检查该文件的语法和位置。

    【讨论】:

      【解决方案3】:

      事实证明,缺少目标条目是罪魁祸首。只需检查我当前的构建目标是否包含 Localizable.string 文件即可解决问题!

      【讨论】:

        【解决方案4】:

        如果您的本地化字符串中有多个变量,您是否可以使用此解决方案:

        在 Localizable.strings 中

        "winpopup" = "#name# wins a #type# and get #points# points(s)"; 
        

        并使用 stringByReplacingOccurrencesOfString 插入值

        NSString *string = NSLocalizedString(@"winpopup", nil); //"#name# wins a #type# and get #points# points(s)"
        NSString *foo = [string stringByReplacingOccurrencesOfString:@"#name#" withString:gameLayer.turn];
        NSString *fooo = [foo stringByReplacingOccurrencesOfString:@"#type#" withString:winMode];
        NSString *msg = [fooo stringByReplacingOccurrencesOfString:@"#points#" withString:[NSString stringWithFormat:@"%i", pkt]];
        NSLog(@"%@", msg);
        

        【讨论】:

        • 这看起来很复杂。您可以只使用编号占位符:"exampleKey" = "%1$@ has bought %3$d apples and %2$d oranges."[NSString stringWithFormat:NSLocalizedString(@"exampleKey", nil), @"Markus", 4, 3] 这将输出:Markus has bought 3 apples and 4 oranges
        • 然后去向世界各地的翻译人员解释这些类型的加密字符串。
        • %1 或 %2 是必须使用的。
        【解决方案5】:

        我只想添加一个我在许多项目中使用的非常有用的定义。

        受 androids 可能性的启发,我已将此功能添加到我的 header prefix 文件中:

        #define NSLocalizedFormatString(fmt, ...) [NSString stringWithFormat:NSLocalizedString(fmt, nil), __VA_ARGS__]
        

        这允许您定义一个本地化字符串,如下所示:

         "ExampleScreenAuthorizationDescriptionLbl"= "I authorize the payment of %@ to %@.";
        

        它可以通过以下方式使用:

        self.labelAuthorizationText.text = NSLocalizedFormatString(@"ExampleScreenAuthorizationDescriptionLbl", self.formattedAmount, self.companyQualifier);
        

        【讨论】:

          【解决方案6】:

          快速:

          let myString = String(format: NSLocalizedString("I authorize the payment of %d ", comment: ""), amount)
          

          【讨论】:

            【解决方案7】:
            extension String {
                public var localizedString: String {
                    return NSLocalizedString(self, comment: "")
                }
            
                public func localizedString(with arguments: [CVarArg]) -> String {
                    return String(format: localizedString, arguments: arguments)
                }
            }
            

            可本地化的字符串:

            "Alarm:Popup:DismissOperation:DeviceMessage" = "\"%@\" will send position updates on a regular basis again.";
            "Global:Text:Ok" = "OK";
            

            用法:

            let message = "Alarm:Popup:DismissOperation:DeviceMessage".localizedString(with: [name])
            

            let title = "Global:Text:Ok".localizedString
            

            【讨论】:

              【解决方案8】:

              这对我有用:

              NSMutableString *testMessage = [NSMutableString stringWithString:NSLocalizedString(@"Some localized text", @"")];
              testMessage = [NSMutableString stringWithString:[testMessage stringByAppendingString:someStringVariable]];
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2019-12-13
                • 1970-01-01
                • 2013-09-15
                • 1970-01-01
                • 2022-11-14
                • 1970-01-01
                • 2014-02-21
                • 1970-01-01
                相关资源
                最近更新 更多