I would like to use localized strings in addition to NSLocale.currentLocale().
Pattern 1:
struct string {
struct localizable {
let locale: NSLocale
init(locale: NSLocale = NSLocale.currentLocale()) {
self.locale = locale
}
/// Locales: en, ja
static func helloWorld() -> String {
return String(format: NSLocalizedString("HelloWorld", comment: ""), locale: self.locale)
}
}
}
Pattern 2:
struct string {
struct en {
/// Locales: en, ja
static func helloWorld() -> String {
return String(format: NSLocalizedString("HelloWorld", comment: ""), locale: NSLocale(localeIdentifier: "en"))
}
}
}
Hey, thanks for creating this issue!
I do plan on adding a feature to R.string for requesting a localized string in a specific locale. Currently, it always chooses the preferred localization.
For now, it's possible to use the generated StringResource to get the strongly typed key for a localized string. You can do something like this:
let basePath = NSBundle.mainBundle().pathForResource("Base", ofType: "lproj")!
let nlPath = NSBundle.mainBundle().pathForResource("nl", ofType: "lproj")!
let baseBundle = NSBundle(path: basePath)!
let nlBundle = NSBundle(path: nlPath)!
let baseTripsTitle = baseBundle.localizedStringForKey(R.string.planner.tripsTitle.key,
value: nil,
table: R.string.planner.tripsTitle.tableName)
let dutchTripsTitle = nlBundle.localizedStringForKey(R.string.planner.tripsTitle.key,
value: nil,
table: R.string.planner.tripsTitle.tableName)
Thank you!
Currently, I am doing the same as your suggestion.
I need time to read and understand the code, but I would like to contribute when I can.
Do you have any idea to implement this feature (pattern 1, pattern 2, or others)?
Any progress on this issue ? Maybe you can give some ideas about how this feature should be implemented, so that someone else can be implemented.
I haven't taken the time to think about this more, since I don't need this at the moment.
The project I'm currently working on does need localised strings for a specific locale, but it's still a few months before we'll start working on that feature.
If someone wants to work on this feature before that, feel free to start a PR or comment on this issue with your ideas.
I'm working on a design for this feature. It will probably be implemented after https://github.com/mac-cain13/R.swift/pull/244 and https://github.com/mac-cain13/R.swift/issues/155.
I think there are two use cases for this, which we can both support:
R.string.settings.en.title() (english title) or R.string.settings.nl.title() (dutch title)NSLocalizedString uses by default.R.string.settings.title(locale: NSLocale(localeIdentifier: "fr-CA")) (french title)With that last use case, I think we should make sure R.swift uses the same fallback logic as NSLocalizedString uses internally. I.e. if a 'en' and 'fr' translation is available, and the users requests 'fr-CA', it should fallback to 'fr'.
I don't think this fallback code is accessible (or open sourced) at this time.
Note that none of this is specific to strings, we could do this for all localized resources.
I've discussed this some more with @mac-cain13, our conclusions so far:
The use cases for static reference to specific locales are small, so this doesn't need to be a top-level API.
Instead of R.string.settings.nl.title(), we can do something like: R.string.settings.title(locale: .nl). Where the '.nl' is a case of an enum specifically generated for the Settings.strings file.
Replicating the complete fallback mechanism of NSLocalizedString is complex, but that can be done outside the scope of this API. If we simply take an array of possible locales, we can use that to fallback, the order can be generated externally, e.g.:
R.string.settrings.title(locales: ["en", "fr", "nl"])
The simplest, most straightforward API, that can also be used for images and the like, is simply an extra argument as the last argument of the generated function.
R.string.settrings.numberOfItemsProgress(current: 1, total: 3, locale: "es")
R.image.logo(compatitibleWith: traitCollection, locale: "zh")
Is there any progress on this or another convenient way to inject the locale? The solution you described on 20 May 2016 could be put in an extension but it seems like a lot of overhead.
Sorry, no progress for me...
Wouldn't it work to overload the static localizable functions with the locale like
static func someLocalizedString(withParam value1: Int, preferredLocale locale: Locale? = nil) -> String {
let bundle = locale?.languageCode.flatMap {
Bundle.main.path(forResource: $0, ofType: "lproj").flatMap {
Bundle(path: $0)
}
} ?? R.hostingBundle
return String(format: NSLocalizedString("SomeLocalizedStringKey", bundle: bundle, value: "String %#@param@", comment: ""), locale: locale ?? R.applicationLocale, value1)
}
Hi, any updates?
An update on this would be great~
This is a useful feature, please release an update
Copied from another issue:
While this feature isn't yet available. As a workaround, this is an extension I currently use in my projects:
import Foundation
import Rswift
extension StringResource {
public func localized(_ language: String) -> String {
guard
let basePath = bundle.path(forResource: "Base", ofType: "lproj"),
let baseBundle = Bundle(path: basePath)
else {
return self.key
}
let fallback = baseBundle.localizedString(forKey: key, value: key, table: tableName)
guard
let localizedPath = bundle.path(forResource: language, ofType: "lproj"),
let localizedBundle = Bundle(path: localizedPath)
else {
return fallback
}
return localizedBundle.localizedString(forKey: key, value: fallback, table: tableName)
}
}
Usage:
R.string.localizable.welcomeMessage.localized("nl")
Another way, without having to change any of the translations is with this:
https://github.com/radianttap/LanguageSwitcher
last update was 2017 though.. I would like to propose this instead https://github.com/marmelroy/Localize-Swift
@happiehappie I've seen that one, but as I understood you would have to update all R.swift strings (we've got 1.5k+). It also looks like it doesn't swizzle NSLocale, so would need code changes for calendars/dates/formatters etc
Closing this issue, this is being implemented in https://github.com/mac-cain13/R.swift/pull/553
Add further comments there.
Most helpful comment
I'm working on a design for this feature. It will probably be implemented after https://github.com/mac-cain13/R.swift/pull/244 and https://github.com/mac-cain13/R.swift/issues/155.
I think there are two use cases for this, which we can both support:
This could be something like:
R.string.settings.en.title()(english title) orR.string.settings.nl.title()(dutch title)NSLocalizedStringuses by default.This could be something like:
R.string.settings.title(locale: NSLocale(localeIdentifier: "fr-CA"))(french title)With that last use case, I think we should make sure R.swift uses the same fallback logic as
NSLocalizedStringuses internally. I.e. if a 'en' and 'fr' translation is available, and the users requests 'fr-CA', it should fallback to 'fr'.I don't think this fallback code is accessible (or open sourced) at this time.
Note that none of this is specific to strings, we could do this for all localized resources.