0

I am using this piece of code to localize dates to Strings in Swift:

if elapsedTime > 3600 { 
        let hourString = NSLocalizedString("hour ago", tableName: "v1.0.0", bundle: Bundle.main, value: "hour ago", comment: "")
        let hoursString = NSLocalizedString("hours ago", tableName: "v1.0.0", bundle: Bundle.main, value: "hours ago", comment: "")
        let hours = Int(elapsedTime/3600)
        return "\(hours) " + (hours > 1 ? hoursString : hourString)
}

where the elapsedTime is a value in seconds. Result for english localization is

6 hours ago for example. I need to modify this to add language specific localization, for example in french it should be not in a format of [N] hours ago but il ya [N] heurs. Is there a way to automate it, avoiding adding specific prefix for different languages at which it used? Thanks in advance!

Olex
  • 1,656
  • 3
  • 20
  • 38
  • 1
    You can use "%@", "%d" and others in `Localizable.string`. Just use a `stringWithFormat:`. Also, there is a plural management with `Localizable.stringsdict`. That should avoid you the `hours >1` test (and for different languages, the text may be different for 0, 1, 2, and more. – Larme Sep 20 '17 at 09:09
  • Thanks, @Larme! Can you please transform your comment to answer, with a bit more explanation about stringWithFormat() usage? – Olex Sep 20 '17 at 09:14
  • In Objective-C, we use `stringWithFormat:`, in Swift, we tend to do like other languages (JavaScript, C++), with the `+` and the `\(var)`. The doc: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html and https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1 for stringDict: https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/StringsdictFileFormat/StringsdictFileFormat.html – Larme Sep 20 '17 at 09:16
  • The other part : https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/InternationalizingYourCode/InternationalizingYourCode.html#//apple_ref/doc/uid/10000171i-CH4-SW1 – Larme Sep 20 '17 at 09:16
  • Possible duplicate of [NSLocalizedString(key: value: comment: ) with variable is not working in swift](https://stackoverflow.com/questions/41905468/nslocalizedstringkey-value-comment-with-variable-is-not-working-in-swift) or also https://stackoverflow.com/questions/39180444/how-to-add-regular-string-placeholders-to-a-translated-plurals-stringdict-in-sw – Larme Sep 20 '17 at 09:18

1 Answers1

0

Based on @Larme comments working version looks like this:

if elapsedTime > 3600 { 
        let hours = Int(elapsedTime / 3600)
        let hourString = NSLocalizedString("hour ago", tableName: "v1.0.0", bundle: Bundle.main, value: "hour ago", comment: "")
        let hoursString = String(format: NSLocalizedString("hours ago", tableName: "v1.0.0", bundle: Bundle.main, value: "\(hours) hours ago", comment: ""),
                           arguments: [hours])
        return (hours > 1 ? hoursString : hourString)
    }

And the change made to v1.0.0.strings(French) is

"hours ago" = "il y a %d heures";
Olex
  • 1,656
  • 3
  • 20
  • 38