简体   繁体   English

从swift中的本地化数值中删除千位分隔符

[英]Remove thousand separator from localized numerical values in swift

I am trying to get localized string along with singular/plural localization from both Localizable.stringsdict and Localizable.strings using String.localizedStringWithFormat() method. 我试图使用String.localizedStringWithFormat()方法从Localizable.stringsdictLocalizable.strings获取本地化字符串以及单数/复数本地化。 But this method returns string with thousand separator/group separator which I don't want. 但是这个方法返回带有thousand separator/group separator字符串,这是我不想要的。 Is there any way to get localized string along with digit localization without thousand separator? 有没有办法获得本地化字符串以及没有千位分隔符的数字本地化? Following is my Localizable.stringsdict : 以下是我的Localizable.stringsdict

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>v1_pineapples_count</key>
        <dict>
            <key>NSStringLocalizedFormatKey</key>
            <string>%#@variable@</string>
            <key>variable</key>
            <dict>
                <key>NSStringFormatSpecTypeKey</key>
                <string>NSStringPluralRuleType</string>
                <key>NSStringFormatValueTypeKey</key>
                <string>d</string>
                <key>zero</key>
                <string>John has no pineapples</string>
                <key>one</key>
                <string>John has 1 pineapple</string>
                <key>other</key>
                <string>John has %d pineapples</string>
            </dict>
        </dict>
    </dict>
</plist>

My code for getting localized string is: 获取本地化字符串的代码是:

    let formatString : String = NSLocalizedString("v1_pineapples_count", comment: "")
    let resultString : String = String.localizedStringWithFormat(formatString, 1000)
 Current output is John has 1,000/١,٠٠٠ pineapples Expected output is John has 1000/١٠٠٠ pineapples 

Since I did not find any way to remove the thousand separator but keep digit localization as well as singular/plural localization, I wrote this extension for my project. 由于我没有找到任何方法来删除千位分隔符,但保持数字本地化以及单数/复数本地化,我为我的项目编写了此扩展。 For now it is serving me well. 现在它很好地为我服务。 Hope it will help someone with same requirements. 希望它能帮助有相同要求的人。

extension String {
    func localizedStringOf(comment: String, args: CVarArg...) -> String {
        let formatter = NumberFormatter()
        formatter.locale = Locale.current

        let formattedString = NSLocalizedString(self, comment: comment)

        let pluralString = withVaList(args){_ in
            (String(format: formattedString, arguments: args) as String)
        }
        var resultString: String  = pluralString
        for arg in args {
            let locDigit = getLocalizedDigit(num: arg as! UInt, formatter:  formatter)
            resultString = replaceString(str: pluralString, num1: arg as! UInt, with: locDigit)
        }

        print(resultString)
        return resultString
    }

    func getLocalizedDigit(num: UInt, formatter: NumberFormatter)->String {
        let number = NSNumber(value: num)
        return formatter.string(from: number) ?? ""
    }

    func replaceString(str: String, num1: UInt, with num2: String ) -> String {
        return str.replacingOccurrences(of: String(num1), with: num2)
    }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM