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

Suggestions to handle Plural Localisation #157

Open
burkaslarry opened this issue Apr 16, 2021 · 1 comment
Open

Suggestions to handle Plural Localisation #157

burkaslarry opened this issue Apr 16, 2021 · 1 comment

Comments

@burkaslarry
Copy link

Given proper setup for localizable string files and dictionaries, when it comes to applying plural localisation in Xcode 12.3 swift 5,

self.lblTopRoom.text = "rooms_unit".localizedPlural(tmp)
gives 1 rooms instead of 1 room

When I drilled into the swift file : String+LocalizedBundleTableName.swift, no values was assigned:

/// bundle & tableName friendly extension
public extension String {

/**
 Swift 2 friendly localization syntax, replaces NSLocalizedString.
 
 - parameter tableName: The receiver’s string table to search. If tableName is `nil`
 or is an empty string, the method attempts to use `Localizable.strings`.
 
 - parameter bundle: The receiver’s bundle to search. If bundle is `nil`,
 the method attempts to use main bundle.
 
 - returns: The localized string.
 */
func localized(using tableName: String?, in bundle: Bundle?) -> String {
    let bundle: Bundle = bundle ?? .main
    if let path = bundle.path(forResource: Localize.currentLanguage(), ofType: "lproj"),
        let bundle = Bundle(path: path) {
        return bundle.localizedString(forKey: self, value: nil, table: tableName)
    }
    else if let path = bundle.path(forResource: LCLBaseBundle, ofType: "lproj"),
        let bundle = Bundle(path: path) {
        return bundle.localizedString(forKey: self, value: nil, table: tableName)
    }
    return self
}

I resolve that using the following method instead . Remember to assign values into localizedString method

extension String {
    func localizedForPlural(_ value: Int) -> String  {
        DLog("current language: \(Localize.currentLanguage())")
        let selectedLanguage =  Localize.currentLanguage()
        //recover the language chosen by the user (in my case, from UserDefaults)
        if let path = Bundle.main.path(forResource: selectedLanguage, ofType: "lproj") , let selectedBundle = Bundle(path: path) {
            let string = NSLocalizedString(self, tableName: nil, bundle: selectedBundle, value: "", comment: "")
            return String(format: string , value)
        }
        return localizedPlural(value)
    }
}

I am looking forward to next version 3.3.0 . Thank you in advance.

@burkaslarry
Copy link
Author

my testing device is iOS 14.4
it works

but, it dies not work on 14.4.2

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

1 participant