3

My iPhone is set to German. My Debug Scheme App Language Setting is set to System Language. Still I have to manually apply the formatter.locale... otherwise I get "Tuesday" instead of "Dienstag". Of course I don't want to set the locale manually. I want the output to be localized to whatever the user chose.

func weekday(forDate day: Date) -> String {
    let formatter = DateFormatter()
    //formatter.locale = Locale.current // gives me locale en
    formatter.locale = Locale(identifier: "de") //without this line I get locale en
    formatter.setLocalizedDateFormatFromTemplate("E")
    return formatter.string(from: day)
}

Are there settings that I don't know about that override the settings I mentioned?

PS: in Playgrounds the locale is correct.

PPS: On my Mac the Simulator's locale is set to en although my Mac system is set to German. Don't know why, didn't mind much yet. What bothers me is that on the iPhone it's not respecting the locale.

Gin Tonyx
  • 383
  • 1
  • 11
  • So are you building for device, not simulator? On Simulator, your Mac's language setting is used as the `Locale`. – Dávid Pásztor Feb 11 '21 at 10:11
  • On my Mac the Simulator's locale is set to en. Don't know why, didn't mind much yet. What bothers me is that on the iPhone it's not respecting the locale. I'll update my post to clarify... Could that point to a problem? – Gin Tonyx Feb 11 '21 at 10:23

3 Answers3

3

I solved the problem by finally finding this post: How do I change the Development language in Xcode before internationalizing my app?

I had to manually edit project.pbxproj inside the .xcodeproj and set the development region to German:

developmentRegion = de;

Now it works without specifying the locale in code and while keeping Scheme set to "System Language".

Gin Tonyx
  • 383
  • 1
  • 11
2

Go to your Target and click Edit Schemes and set your app language to "German" from option tab as ss below.

enter image description here

zeytin
  • 5,545
  • 4
  • 14
  • 38
  • Isn't that about the same as specifying it in code? I want it to support all locales that DateFormatter knows... – Gin Tonyx Feb 11 '21 at 10:35
  • if you try first of all, you will see is it same or not – zeytin Feb 11 '21 at 10:36
  • still eng ? you right then it is interesting. – zeytin Feb 11 '21 at 11:03
  • I effed up. I changed debug and not run scheme. When I change run scheme the locale changes in simulator, in preview and on device to the language I select in scheme. My bad. – Gin Tonyx Feb 11 '21 at 11:28
  • Is this how it is supposed to work? What if I user decides to change locale and wants English now? I thought this was supposed to be dynamic? – Gin Tonyx Feb 11 '21 at 13:46
  • of course you will manipulate via your codes, this is only for default values which makes it German – zeytin Feb 11 '21 at 14:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228577/discussion-between-gin-tonyx-and-zeytin). – Gin Tonyx Feb 11 '21 at 14:39
  • I actually forgot that I hardcoded the region. That reminded me, Thank you – Abdul Waheed Jul 18 '22 at 20:51
0

For those who not want to edit the pbxproj config file each time when testing different locales, here is an another approach (XCode14, IOS16.2):

  • info.plist contains the entry CFBundleDevelopmentRegion set to $(DEVELOPMENT_LANGUAGE)
  • Delete the entry

This allows the expected behaviour - set region and language in your Xcode scheme, or leave it at system language and system region to use the device settings.

theExpert
  • 51
  • 1
  • 5