Gokhan Turkben
Gokhan Turkben

Reputation: 101

IOS localized sound file not working on device

I am working on a multi language app. This app includes some sound files. For examples if users system language is English, when user click on apple icon, pronunciation of "apple" name will play from /base.lproj/apple.mp3 file. if users system language is Turkish, pronunciation of "apple" name will play in Turkish from /tr.lproj/apple.mp3 file.

enter image description here

Localizable.string, Main.string, InfoPlist.strings and all sound files are localized and project is working fine at simulator.

However when we come to real device to test, sound files are not localized according to system language. and only play base language (English) sound file.

I think I found problem but I dont know the solution.

To play sound, first i found sound URL like that:

 let path = Bundle.main.path(forResource: name, ofType: "mp3")
 print(path)
 let soundURL = URL(fileURLWithPath: path!)

When i print sound url in simulator, simulator finds correct sound file according to language:

/Bundle/Application/.../tr.lproj/apple.mp3"  //Applang: Turkish - Play Tur
/Bundle/Application/.../base.lproj/apple.mp3"  //Applang: System - Play Eng

But when i print sound url in real device which language is Turkish, it finds base language file (English) and print this path.

/Bundle/.../grapefruit.mp3" // where is tr.lproj ?

Again Localizable.string (Turkish), Main.string(Turkish), InfoPlist.strings(Turkish) work good and show text correctly in their own langauge on real device.

According to Apple Document

path(forResource:ofType:) : The method first looks for a matching resource file in the non-localized resource directory of the specified bundle. If a matching resource file is not found, it then looks in the top level of an available language-specific .lproj folder.

so i understand that i should use another path method

path(forResource:ofType:inDirectory:forLocalization:)

and specified my code like that

let path = Bundle.main.path(forResource: name, ofType: "mp3",inDirectory:"tr.lproj",forLocalization:"tr")

Is there any other way for localized sound files properly in device? Or should i use path(forResource:ofType:inDirectory:forLocalization:) method and build some logical code. (check system language etc)

Upvotes: 0

Views: 742

Answers (2)

Alex
Alex

Reputation: 159

It was a known bug in IOS 9. iOS localization is broken with the upgrade iOs 9 + Xcode 7

It looks like it has comeback to IOS 14...
As a workaround you can use the localized bundle, like so.

Use something like this. ** Localize.currentLanguage() return the string of the language you need, aka "it".

   @objc static func getLocalizedBundle() -> Bundle {
        let bundle: Bundle = .main
        if let path = bundle.path(forResource: Localize.currentLanguage(), ofType: "lproj") {
            if let locolizedBundle = Bundle(path: path) {
                return locolizedBundle
            } else {
                return bundle
            }
        }
        return bundle
    }

then you can use it like so.

  NSURL *audioFileLocationURL;
        audioFileLocationURL = [[Localize getLocalizedBundle] URLForResource:soundName withExtension:nil];
        //fallback
        if (!audioFileLocationURL) {
            audioFileLocationURL = [[NSBundle mainBundle] URLForResource:soundName withExtension:nil];
        }

Upvotes: 0

Damien
Damien

Reputation: 3362

I tried to use localized mp3 in a test app and it works fine on a real device. what exactly do you pass as name in let path = Bundle.main.path(forResource: name, ofType: "mp3") ? If you have an other file in your project called like that it may take this one everytime !

If you don't have any file named like that in your project navigator other than those who are localized, try to uninstall your app, maybe there's still one in the app directories on your device.

Upvotes: 1

Related Questions