divadretlaw / CustomAlert

🔔 Custom Alert for SwiftUI
MIT License
195 stars 18 forks source link

Localize Text, Content & Buttons #7

Closed mobileuidev closed 1 year ago

mobileuidev commented 1 year ago

Currently I need to do something like the following to get the localized text for the alert:

extension String {
  func getLocalizedTextForAlert() {
        let path = Bundle.main.path(forResource: "ar", ofType: "lproj")
        let bundle = Bundle(path: path!)
        return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
  }
}

where ar is one of the supported languages in my app (Arabic), because doing just: Text("some text") will always fallback to the English version of the some text text. Giving that I have two Localizable.strings files in the app: one for en and the other for ar localization.

divadretlaw commented 1 year ago

Hi, I see there is an issue while providing the title. There are 3 variants of providing the title for the Custom Alert. One with Title, one with LocalizedStringKey and one with StringProtocol

With the first one (Title), localization works like any Text. Text("some string") will be localized as long as "some string" is a key in your Localizable.strings

.customAlert(Text("Title"), isPresented: $showAlert) {
    Text("My localized Message")
} actions: {
    Button(role: .cancel) {

    } label: {
        Text("OK")
    }
}

In this example everything is localized, as long as "Title", "My localized Message" and "OK" are String Keys in Localizable.strings

The issue I see is that the third (StringProtocol) one is preferred over the second (LocalizedStringKey) one at the moment. This will be fixed in v2.3.0 the current workaround would be. Then again everything is localized, as long as "Title", "My localized Message" and "OK" are String Keys in Localizable.strings

.customAlert("Title" as LocalizedString, isPresented: $showAlert) {
    Text("My localized Message")
} actions: {
    Button(role: .cancel) {

    } label: {
        Text("OK")
    }
}

The localization of the Content and Buttons should just work like any localization with any Text in SwiftUI, it is up to the integrator how they want to provide Localization. Text("some string") will be localized as long as "some string" is a key in your Localizable.strings

divadretlaw commented 1 year ago

Title should be fixed with v2.3.0. The Localization of Content and Buttons is up to the integrator and should just work like any other localization in your app.

mobileuidev commented 1 year ago

TestLocalizedAlert.zip

Thank you for the prompt reply, however, I didn't mention that when the iPhone's main language in Settings app is set to English then the alert won't localize the Title, Message and Action buttons to Arabic even if the strings are added in the Localizable.strings files, and it localizes to the other language (Arabic) as expected when the iPhone's main language is set to Arabic. (i didn't use the third problematic initializer you mentioned above)

My real app gives the user the option to set its locale to English or Arabic regardless to the main iPhone's language through injecting the @Environment(\.locale) into the main app's view (ContentView in the attached sample project), you will notice how "Hello World" text is successfully localized to Arabic regardless to the iPhone's language but the alert's title is not.

check the attached sample and see how the alert doesn't localize the Title to Arabic when the iPhone's main language is set to English.

divadretlaw commented 1 year ago

I see, thank you for the example. That helps a lot. The way CustomAlert works is that the CustomAlert is in its own UIWindow, but the SwiftUI Environment is not automatically transferred to it, since it's a completely new root view.

The SwiftUI.Environment of the calling view will be propagated in v2.4.0 which should fix your issue. If not please let me know.

mobileuidev commented 1 year ago

yes v2.4.0 did exactly that and the issue is fixed. Thank you so much for your quick fix.