Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSString.localizedStringWithFormat to take stringsdict pluralization into account #91

Closed
AliSoftware opened this issue Feb 17, 2016 · 6 comments

Comments

@AliSoftware
Copy link
Collaborator

Use NSString.localizedStringWithFormat(format, args) (instead of just String(format:arguments:) in the strings templates, to take .stringsdict pluralization into account.

See @marmelroy's talk at dotSwift'16 here.

@AliSoftware
Copy link
Collaborator Author

Actually according to the documentation, I'm not so sure that String(format: arguments:) doesn't do that already (taking .stringdict files and pluralization into account according to the value of the numeric parameters)… so maybe there is no need to change anything after all?

@marmelroy
Copy link

Cool question...

So when you look at the implementation over at https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/String.swift:

    public init(format: String, arguments: [CVarArgType]) {
        self = String(format: format, locale: nil, arguments: arguments)
    }

whereas localizedStringWithFormat is implemented as:

    public static func localizedStringWithFormat(
        format: String, _ arguments: CVarArgType...
        ) -> String {
        return String(format: format, locale: NSLocale.currentLocale(),
                              arguments: arguments)
    }

According to Apple locale=nil is equivalent to locale=NSLocale.systemLocale() and:

Use the system locale when you don’t want any localizations. Use the current locale to format text that you display to users.

@marmelroy
Copy link

So localizedStringWithFormat is the correct one.

@AliSoftware
Copy link
Collaborator Author

Ah! Good thinking looking into the Swift OSS source code! Thanks for digging into it!

@AliSoftware
Copy link
Collaborator Author

Still feels non-Swifty to me to use a static func when an init is present…

I think I'll use return String(format: format, locale: NSLocale.currentLocale(), arguments: arguments) directly (so I actually use an initializer) instead of calling the static func return String.localizedStringWithFormat(format, arguments)… see any drawback in doing that?

@marmelroy
Copy link

I see your point... there is something quite ObjC-y about it. 😄

Anyway, there's zero difference between the two. Go with what looks nicer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants