# 关于本地化

## localization包含的文件

localization的文件包括本地化string以及本地化图片.

工程中设置了本地化后,在info中可以看到,每种language的resources file个数并不相同(在资源文件相同的情况下也不相同).那么为什么英文的会多呢?多的是什么?

![](/files/-LAdZWROmB_Z_mb0s15C)

* 新建一个工程,查看localization的内容会发现英文有两个文件,这是因为使用英文作为development language.在创建工程时,会默认生成两个development language的文件算作本地化文件.这两个文件是Main.storyboard和 LaunchScreen.storyboard
* Base.lproj中的内容会算作development language的文件.

这就是为什么作为development language英文的会比其他语言的文件多了的原因.

## 当前系统语言  和 当前app显示语言

有本地化以后,在工程中经常要进行语言判断,常用的是判断当前系统语言,但是也会用到判断当前app显示语言的场景.

```
 [[NSLocale preferredLanguages] objectAtIndex:0];
```

这个方法可以获取当前系统设置语言,但是并不是本地化后app显示的语言(就是UI显示的语言),来寻找一下实现这个的方法,在NSLocale.h文件中,可以查看注释

```
@property (class, readonly, copy) NSArray<NSString *> *preferredLanguages NS_AVAILABLE(10_5, 2_0);
 // note that this list does not indicate what language the app is actually running in; 
 //the NSBundle.mainBundle object determines that at launch and knows that information
```

这个数组是根据设置中的首选语言顺序给出的数组.

![](/files/-LAdZWSb5X1pOPXlm2FK)

可以注意到,注释中解释app真正running in 的语言需要在 NSBundle.mainBundle中寻找.查看NSBundle.h可以找到以下代码

```
/* Methods for dealing with localizations. */
@property (readonly, copy) NSArray<NSString *> *preferredLocalizations;  
  // a subset of this bundle's localizations, re-ordered into the preferred 
  //order for this process's current execution environment; the main bundle's preferred 
  //localizations indicate the language (of text) the user is most likely seeing in the UI
```

实际上,app是根据上面那个首选语言顺序,依次查看当前是否有该语言的本地化,直到找到对应有本地化的语言或顺序查找完毕,如果所有首选语言都没有没本地化,将使用程序设置的默认语言,所以我们可以使用这个方法来获取

```
 [[[NSBundle mainBundle] preferredLocalizations] firstObject]
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://philm.gitbook.io/philm-ios-wiki/mei-zhou-yue-du/guan-yu-ben-di-hua.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
