iOS. Pluralization. Stringdict with format that contains 2 int arguments

后端 未结 1 1972
刺人心
刺人心 2021-01-20 14:58

My question is similar to How to add regular string placeholders to a translated plurals .stringdict in swift ios but I am trying to understand if it is possible to pass 2 i

1条回答
  •  太阳男子
    2021-01-20 15:38

    One format is sufficient. You can use multiple placeholders in the NSStringLocalizedFormatKey entry, and for each placeholder a separate dictionary with the plural rule. Example:

    
    
    
        
            apples_and_pears
            
                NSStringLocalizedFormatKey
                %#@num_apples@ : %#@num_pears@
                num_apples
                
                    NSStringFormatSpecTypeKey
                    NSStringPluralRuleType
                    NSStringFormatValueTypeKey
                    ld
                    zero
                    no apple
                    one
                    1 apple
                    other
                    %ld apples
                
                num_pears
                
                    NSStringFormatSpecTypeKey
                    NSStringPluralRuleType
                    NSStringFormatValueTypeKey
                    ld
                    zero
                    no pear
                    one
                    1 pear
                    other
                    %ld pears
                
            
        
    
    

    Usage:

    let apples = 1
    let pears = 3
    let format = NSLocalizedString("apples_and_pears", comment: "")
    let applesAndPears = String.localizedStringWithFormat(format, apples, pears)
    print(applesAndPears) // 1 apple : 3 pears
    

    This can be combined with positional parameters: if the format is changed to

            NSStringLocalizedFormatKey
            %2$#@num_pears@ : %1$#@num_apples@
    

    then the output becomes “3 pears : 1 apple”.

    0 讨论(0)
提交回复
热议问题