0

Hi I have an imageView to show some Pictures. I want to show different pictures for every language. So when the Users localization is German, he gets different pictures than a User with the localization English.

This is my code. How can I make several imageArrays for every localization?

thanks

override func viewDidLoad() {

    super.viewDidLoad()

    mainScrollView.frame = view.frame


    imageArray = [#imageLiteral(resourceName: "help0"), #imageLiteral(resourceName: "help1"), #imageLiteral(resourceName: "help2"), #imageLiteral(resourceName: "help3"), #imageLiteral(resourceName: "help4")]



    for i in 0..<imageArray.count{

        let imageView = UIImageView()
        imageView.image = imageArray[i]
        imageView.contentMode = .scaleAspectFit
        let xPosition = self.view.frame.width * CGFloat(i)

        imageView.frame = CGRect(x: xPosition , y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)



        mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
        mainScrollView.addSubview(imageView)

        self.view.sendSubview(toBack: mainScrollView)

    }
Feindpak
  • 45
  • 1
  • 6
  • 1
    Nice answer here: https://stackoverflow.com/questions/21310819/how-to-localize-the-images-in-images-xcassets – Norman Nov 30 '17 at 21:00

2 Answers2

1

You can do it like this by using the NSLocalizedString, so you declare you array like this:

let help0 = UIImage(named: NSLocalizedString("help0", comment: ""))
let help1 = UIImage(named: NSLocalizedString("help1", comment: ""))
let help2 = UIImage(named: NSLocalizedString("help2", comment: ""))
let help3 = UIImage(named: NSLocalizedString("help3", comment: ""))
let help4 = UIImage(named: NSLocalizedString("help4", comment: ""))

let imageArray = [help0, help1, help2, help3, help4]

Then you add your localization langiages in your example ENG and GER and in those files you add the image you want to use, like this (a guide of how to add localization):

Localization for English:
"help0" = "help0Eng";
"help1" = "help1Eng";
etc...

Localization for German:
"help0" = "help0Ger";
"help1" = "help1Ger";
etc...

And your help0Eng, help0Ger etc. is the name of the images in your Assets.xcassets. So the following will happen:

  1. NSLocalizedString("help0", comment: "") will be localzied in the right file for the selected language
  2. That image name will be used for help0
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
0

Locale has what you’re looking for. During development, you can use Locale.isoLanguageCodes to get an array of all possible language codes (each code is a two- or three-letter string). At runtime, use Locale.current.languageCode to return the language code for your user’s current language.

To implement this, you could have a helper function along the lines of:

func localizedImageArray() -> [UIImage] {
    let currentLanguage = Locale.current.languageCode
    let imageArray: [UIImage]

    if currentLanguage == "ja" {
        [#imageLiteral(resourceName: "help0_ja"), #imageLiteral(resourceName: "help1_ja"), #imageLiteral(resourceName: "help2_ja"), #imageLiteral(resourceName: "help3_ja"), #imageLiteral(resourceName: "help4_ja")]
    } else if currentLanguage == "kr" {
        [#imageLiteral(resourceName: "help0_kr"), #imageLiteral(resourceName: "help1_kr"), #imageLiteral(resourceName: "help2_kr"), #imageLiteral(resourceName: "help3_kr"), #imageLiteral(resourceName: "help4_kr")]
    } else /* Default implementation */ {
        [#imageLiteral(resourceName: "help0"), #imageLiteral(resourceName: "help1"), #imageLiteral(resourceName: "help2"), #imageLiteral(resourceName: "help3"), #imageLiteral(resourceName: "help4")]
    }

    return imageArray
}

In your viewDidLoad(), instead of declaring imageArray = /* the part where you set the array with literals */, you would declare imageArray = localizedImageArray()

Please note that strings should not be localized using this method. For localized strings, use NSLocalizedString.

Alan Kantz
  • 365
  • 1
  • 2
  • 11