How to get current language code with Swift?

前端 未结 13 2082
难免孤独
难免孤独 2020-12-07 11:06

I want get the language code of the device (en, es...) in my app written with Swift. How can get this?

I\'m trying this:

var preferredLanguages : NSL         


        
13条回答
  •  一个人的身影
    2020-12-07 11:48

    Swift 3 & 4 & 4.2 & 5

    Locale.current.languageCode does not compile regularly. Because you did not implemented localization for your project.

    You have two possible solutions

    1) String(Locale.preferredLanguages[0].prefix(2)) It returns phone lang properly.

    If you want to get the type en-En, you can use Locale.preferredLanguages[0]

    2) Select Project(MyApp)->Project (not Target)-> press + button into Localizations, then add language which you want.

提交回复
热议问题