tofucodes diary

にほんごのほう

続報:iOS 11からNSLocaleの仕様が変更されたらしい

以前iOS 11のNSLocaleについて書いた記事はこちら。

10月にAppleのDeveloper Forumに投稿していた質問を久しぶりに見てみたら2月に返信がありました。

翻訳しながら内容を見てみます。


I think the change in behavior makes sense sort of for many things, but would be nice if there was a way to opt out and get the real device locale even if your app doesn't support the user's native language.

  • 今回の変更はまあいろんな意味で理にかなってると思うけど
  • アプリがその言語をサポートしてなくてもユーザのデバイスの実際のロケールを取得できるような方法があったら良かったね

For example something like this:

  • 例えばこんな感じで
+(NSLocale*)deviceLocale; //Device locale, even if the app is not localized in the given language. 

My app displays dates and I've had a couple users complain that they want the date formatted in the style of their language. Even though my app isn't localized in their language, they don't care, they want the date the way they want it.

Does anyone know of a way to get the device locale like +currentLocale used to?

  • 私のアプリは日付を表示してるけど、ユーザの言語のスタイルでフォーマットされた日付が欲しいと訴えてきたユーザが幾人かいました
  • たとえ私のアプリが彼らの言語にローカライズされていなくても、彼らはそんなこと気にせず、彼らの望むフォーマットの日付が欲しいのです
  • 以前+currentLocaleでできたように、デバイスの実際のロケールを取得する方法をどなたか知りませんか?

Edit to answer my own question, below seems to work:

  • 自分の質問に自分で回答します。以下の方法でうまくできそうです
NSString *localeID = NSLocale.preferredLanguages.firstObject;  
NSLocale *deviceLocale = [NSLocale localeWithLocaleIdentifier:localeID];  
//use the locale  

上記の方法はまだ実際に試していないので時間があったら試してみようと思いますが、今までcurrentLocaleを利用していたロジックを上記に置き換えることで、iOS 11以降でもアプリのローカライズに関係なく端末の言語設定に準じたロケールを取得することができそうです。