代码之家  ›  专栏  ›  技术社区  ›  futureelite7 Adam Rosenfield

是否可以将变量和/或参数与NSLocalizedString一起使用?

  •  59
  • futureelite7 Adam Rosenfield  · 技术社区  · 15 年前

    我已经尝试使用一个变量作为NSLocalizedString的输入参数,但是我得到的只是输入参数。我做错什么了?是否可以使用变量字符串值作为本地化字符串的索引?

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

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

    案例2:格式化的NSLocalizedString:

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

    谢谢!

    7 回复  |  直到 15 年前
        1
  •  123
  •   Wevah    15 年前

    如果您想要返回本地化版本的“This is a 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 %@";
    
        2
  •  22
  •   Alexander    11 年前

    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);
    
        3
  •  9
  •   Xero    8 年前

    let myString = String(format: NSLocalizedString("I authorize the payment of %d ", comment: ""), amount)
    
        4
  •  4
  •   futureelite7 Adam Rosenfield    14 年前

        5
  •  3
  •   Michal Zaborowski    6 年前
    extension String {
        public var localizedString: String {
            return NSLocalizedString(self, comment: "")
        }
    
        public func localizedString(with arguments: [CVarArg]) -> String {
            return String(format: localizedString, arguments: arguments)
        }
    }
    

    Localizable.string:

    "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
    
        6
  •  2
  •   zeiteisen    13 年前

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

    在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);
    
        7
  •  0
  •   JWWalker    15 年前

        8
  •  0
  •   Vladimir Kuzomenskyi    4 年前

    这对我很有用:

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