首页 iOS.& Swift Books iOS.Apprentice

41
国际化 由Eli Ganim撰写

到目前为止,在本书中所做的应用程序都是英文。毫无疑问,美国是亚洲最大的应用市场最大的市场市场。口袋如果您加入所有较小的国家,英语不是主要语言的较小国家,您仍然最终有开奖结果3d非常相当大的市场,您可能会错过。

幸运的是,iOS使得为其他语言添加支持非常容易,这是开奖结果3d已知的过程 国际化。这通常缩写为“i18n”,因为这是写作的较短; 18代表I和N之间的字母数。你还经常听到这个词 本土化,这意味着有点相同。

在本章中,要使您的脚弄湿本地化,您将增加对荷兰语的支持。您还将更新Web服务查询以返回针对用户的区域设置优化的结果。

您将涵盖以下项目:

  • 添加新语言:如何为应用程序添加新显示语言(显示文本)的支持。
  • 本地化屏幕文本:如何本地化代码中使用的文本值。
  • Infoplist.strings.:本地化Info.Plist文件设置,如应用名称。
  • 区域设置:修改Web查询以发送设备语言和区域以获取本地化的搜索结果。

添加新语言

此时,您的源代码文件夹的结构可能看起来像这样:

源代码文件夹中的文件
源代码文件夹中的文件

有开奖结果3d名叫的子文件夹 base.lproj. 至少包含故事板, main.storyboard.。 base.lproj文件夹是可以本地化的文件。到目前为止,这可能只是故事板,但你很快就会向这个文件夹添加更多文件。

当您为另一种语言添加支持时,开奖结果3d新的 xx.lproj. 使用XX创建文件夹是该新语言的双字母代码 - EN. 英语, NL. for Dutch etc.

本地化开奖结果3dnib文件

让我们首先本地化开奖结果3d简单的文件, notherfoundcell.xib.。通常nib文件包含需要翻译的文本。您可以简单地为特定语言制作现有的NIB文件的新副本,并将其放在右侧.lproj文件夹中。当iPhone使用该语言时,它将自动加载翻译的笔尖。

Nottrefoundcell没有本地化
Sve Vijcutzkuorjkoqw Kok Su Fibuyujopooqd

Xcode询问移动文件是否可以
MTESE OYYT PWEPXEX IB的AY FO yaji LHI NESO

Xcode将NoverFoundCell.xib移动到en.lproj文件夹
ghosi teboz kagbohkfuovqlocx.qaj di mna牛.cxqum jubhak

本地化部分现在包含英语条目
gna xuqidegoxeuz dozcaex他的quzxeiwh uf oqjnv buv impsaxk

为新语言添加支持

要为您的应用添加对新语言的支持,您必须切换到 项目设置 screen.

项目设置
HDO YMEGESK Taprovyq.

添加新语言
Iyrodz o xaq kayqiuqe

选择要本地化的文件
gmoufehn bgi rowuw zu huqupexe

notherfoundcell.xib有两个本地化
gojwuwdhiezhgebd.cob bod fwo vawubenukaost

编辑特定语言的笔尖

让我们编辑这个nib的荷兰语版本。

这就是你在荷兰语中说的方式
qvuf'l hig hui xez ih ih yabww

切换设备语言

➤从模拟器中删除应用程序。做开奖结果3d干净的(产品▸清洁 或者 Shift-⌘-K)并重新构建应用程序。

在模拟器中切换语言
Lwifmvebf Relfaukut UT Thu Cirineceg

如果那样,我会感到惊讶
U's fu hohnxaxax前mpad xep leqy ej i fuqfs

基地国际化

要本地化其他脚步,可以重复该过程并将其XIB文件的副本添加到 NL..lproj. 文件夹。这对这个应用程序的方法来说不是太糟糕,但如果您有开奖结果3d具有真正复杂的屏幕的应用程序,则拥有多个同一笔尖的副本可以成为维护噩梦。

选择基础本地化为目的地
Vruenatj RMO Quli Lupucaxivaex IC FPI FitMonubeag

添加荷兰定位
Epsorb e ceqbf bunaguxadaoh

荷兰本地化是开奖结果3d字符串文件
ywu zajbl woqutuvuxaet uq a zktafgc liye

/* Class = "UILabel"; text = "Loading..."; ObjectID = "hU7-Dc-hSi"; */
"hU7-Dc-hSi.text" = "Loading...";
助理编辑器显示翻译的预览
LFI Uzyafdojf EKAFAF FTATM I QSAYEOQ UL STO PGONTDEWEEP

本地化的加载文本
Rwa Jejebafet Zuaxexq Betr

"68e-CH-NSs.placeholder" = "Naam van artiest, nummer, album";
"Sjk-fv-Pca.segmentTitles[0]" = "Alles";
"Sjk-fv-Pca.segmentTitles[1]" = "Muziek";
"Sjk-fv-Pca.segmentTitles[2]" = "Software";
"Sjk-fv-Pca.segmentTitles[3]" = "E-boeken";
本地化的SearchViewController.
gta bafovezam maetwmcooplowfnetvas.

"DCQ-US-EVg.text" = "Soort:";
"ZYp-Zw-Fg6.text" = "Genre:";
"yz2-Gh-kzt.text" = "Kind Value";
"Ph9-wm-1LS.text" = "Artist Name";
"JVj-dj-Iz8.text" = "Name";
"7sM-UJ-kWH.text" = "Genre Value";
"xOH-GC-bHs.normalTitle" = "$9.99";
荷兰人的弹出窗口
BBO WUV-UY UK JUXDV

本地化屏幕文本

Even though the nibs and storyboard have been translated, not all of the text is. For example, in the one-before-the-previous image the text from the kind property is still “Song.”

用于代码中使用的本地化文本

要本地化不在笔记或故事板中的文本,您必须使用另一种方法。

import Foundation
var type:String {
  let kind = self.kind ?? "audiobook"
  switch kind {
  case "album":
    return NSLocalizedString("Album", 
                    comment: "Localized kind: Album")
  case "audiobook":
    return NSLocalizedString("Audio Book", 
                    comment: "Localized kind: Audio Book")
  case "book":
    return NSLocalizedString("Book", 
                    comment: "Localized kind: Book")
  case "ebook":
    return NSLocalizedString("E-Book", 
                    comment: "Localized kind: E-Book")
  case "feature-movie":
    return NSLocalizedString("Movie", 
                    comment: "Localized kind: Feature Movie")
  case "music-video":
    return NSLocalizedString("Music Video", 
                    comment: "Localized kind: Music Video")
  case "podcast":
    return NSLocalizedString("Podcast", 
                    comment: "Localized kind: Podcast")
  case "software":
    return NSLocalizedString("App", 
                    comment: "Localized kind: Software")
  case "song":
    return NSLocalizedString("Song", 
                    comment: "Localized kind: Song")
  case "tv-episode":
    return NSLocalizedString("TV Episode", 
                    comment: "Localized kind: TV Episode")
  default:
    return kind
  }
}
return "Album"
return NSLocalizedString("Album", comment: "Localized kind: Album")

生成可本地化的文本字符串

➤ Open a Terminal, cd to the folder that contains the Storesearch. 项目。您要进入包含实际源文件的文件夹在我的系统上是:

cd ~/Desktop/StoreSearch/StoreSearch
genstrings *.swift -o en.lproj
/* Localized kind: Album */
"Album" = "Album";

/* Localized kind: Software */
"App" = "App";

/* Localized kind: Audio Book */
"Audio Book" = "Audio Book";

/* Localized kind: Book */
"Book" = "Book";

/* Localized kind: E-Book */
"E-Book" = "E-Book";

/* Localized kind: Feature Movie */
"Movie" = "Movie";

/* Localized kind: Music Video */
"Music Video" = "Music Video";

/* Localized kind: Podcast */
"Podcast" = "Podcast";

/* Localized kind: Song */
"Song" = "Song";

/* Localized kind: TV Episode */
"TV Episode" = "TV Episode";
"Song" = "SUPER HIT!";
它曾经说过歌曲现在说超级打击!
vhoha oy icig ka zox sawq ed tar cavf webom xez!

"Album" = "Album";
"App" = "App";
"Audio Book" = "Audioboek";
"Book" = "Boek";
"E-Book" = "E-Boek";
"Movie" = "Film";
"Music Video" = "Videoclip";
"Podcast" = "Podcast";
"Song" = "Liedje";
"TV Episode" = "TV serie";
// DetailViewController, updateUI()
artistNameLabel.text = "Unknown"
priceText = "Free"

// LandscapeViewController, showNothingFoundLabel()
label.text = "Nothing Found"

// SearchResultCell, configure(for)
artistNameLabel.text = "Unknown"

// SearchViewController, showNetworkError()
title: "Whoops...",
message: "There was an error reading from the iTunes Store. 
          Please try again.",
title: "OK"
let alert = UIAlertController(
  title: NSLocalizedString("Whoops...", 
  comment: "Error alert: title"), message: NSLocalizedString(
  "There was an error reading from the iTunes Store. Please try again.", comment: "Error alert: message"), preferredStyle: .alert)
Warning: Key "Unknown" used with multiple comments "Artist name label: Unknown" & "Artist name: Unknown"
"Nothing Found" = "Niets gevonden";

"There was an error reading from the iTunes Store. Please try again." = "Er ging iets fout bij het communiceren met de iTunes winkel. Probeer het nog eens.";

"Unknown" = "Onbekend";

"Whoops..." = "Foutje...";
let s = NSLocalizedString("ERROR_MESSAGE23", 
                 comment: "Error message on screen X")
/* Error message on screen X */
"ERROR_MESSAGE23" = "Does not compute!";

本地化动态构造的字符串

如果您的应用程序动态构建字符串,那么您也可以本地化此类文本。例如,在 searchresultcell.swift., configure(for:) you do:

artistNameLabel.text = String(format: "%@ (%@)", 
   searchResult.artistName, searchResult.kindForDisplay())
artistNameLabel.text = String(format: 
  NSLocalizedString("%@ (%@)", 
  comment: "Format for artist name"), 
  searchResult.artistName, searchResult.kindForDisplay())
/* Format for artist name */
"%@ (%@)" = "%[email protected] (%[email protected])";
"%@ (%@)" = "%[email protected] van %[email protected]";
现在的“善良”是第开奖结果3d,艺术家名字持续
XSU“摇滚”Quk Lutew Nummz,Sto imlukw velze husn

/* Format for artist name label */
"ARTIST_NAME_LABEL_FORMAT" = "%[email protected] van %[email protected]";
/* Format for artist name label */
"ARTIST_NAME_LABEL_FORMAT" = "%[email protected] (%[email protected])";
artistNameLabel.text = String(format: 
  NSLocalizedString("ARTIST_NAME_LABEL_FORMAT", 
  comment: "Format for artist name label"), 
  searchResult.artistName, searchResult.kindForDisplay())

数据驱动的本地化

还有一件事我想改善。记住如何 searchresult.swift. the type property is this enormous switch statement? That’s “smelly” to me. The problem is that any new products require you to add another case to the switch.

private let typeForKind = [
  "album": NSLocalizedString("Album", 
                    comment: "Localized kind: Album"),
  "audiobook": NSLocalizedString("Audio Book", 
                        comment: "Localized kind: Audio Book"),
  "book": NSLocalizedString("Book", 
                   comment: "Localized kind: Book"),
  "ebook": NSLocalizedString("E-Book", 
                    comment: "Localized kind: E-Book"),
  "feature-movie": NSLocalizedString("Movie", 
                   comment: "Localized kind: Feature Movie"),
  "music-video": NSLocalizedString("Music Video", 
                 comment: "Localized kind: Music Video"),
  "podcast": NSLocalizedString("Podcast", 
                      comment: "Localized kind: Podcast"),
  "software": NSLocalizedString("App", 
                       comment: "Localized kind: Software"),
  "song": NSLocalizedString("Song", 
                   comment: "Localized kind: Song"),
  "tv-episode": NSLocalizedString("TV Episode", 
                         comment: "Localized kind: TV Episode"),
]
var type: String {
  let kind = self.kind ?? "audiobook"
  return typeForKind[kind] ?? kind
}

Infoplist.strings.

应用程序本身可以具有不同的名称,具体取决于用户的语言。 iPhone主屏幕上显示的名称来自于此 捆绑名称 设置 info.plist. 或者如果存在,那么 捆绑显示名称 setting.

将新字符串文件添加到项目中
oycohs我sep kmsukfb seqo le jtu cvuzibg

CFBundleDisplayName = "StoreZoeker";
即使是应用程序的名称也是本地化的!
Ucid HNU EJD'X Bado EK Mixanumap!

区域设置

在一些早期的屏幕截图中,您可能已经注意到即使您将语言转换为荷兰语,产品的价格仍然以美元而不是欧元。这是有两个原因:

修复Web请求包含语言和区域

您将修复该应用程序,以便它将有关用户的语言和区域设置的信息发送到erterunes商店。

private func iTunesURL(searchText: String, 
                         category: Category) -> URL {
  // Add the following 3 lines
  let locale = Locale.autoupdatingCurrent
  let language = locale.identifier
  let countryCode = locale.regionCode ?? "US"
  . . .
  // Modify the URL string
  let urlString = "//itunes.apple.com/search?" + 
    "term=\(encodedText)&limit=200&entity=\(kind)" + 
    "&lang=\(language)&country=\(countryCode)"
    
  let url = URL(string: urlString)
  print("URL: \(url!)")            // Add this
  return url!
}
//itunes.apple.com/search?term=bird&limit=200&entity=&lang=en_US&country=US

测试区域变化

➤在模拟器中,切换到 设置 应用程序更改区域设置。去 一般▸语言& Region ▸ Region。选择 荷兰.

//itunes.apple.com/search?term=bird&limit=200&entity=&lang=nl_NL&country=NL
价格根据用户的区域设置
rte jjowa uxramfawx za myi uyaw'g biheiz zufzictw

有开奖结果3d技术问题?想报告开奖结果3d错误吗? 您可以向官方书籍论坛中的书籍作者提出问题和报告错误 这里.

有反馈分享在线阅读体验吗? 如果您有关于UI,UX,突出显示或我们在线阅读器的其他功能的反馈,您可以将其发送到设计团队,其中表格如下所示:

© 2021 Razeware LLC

您可以免费读取,本章的部分显示为 混淆了 文本解锁此博克,以及我们的整个书籍和视频目录,具有raywenderlich.com的专业敏感。

现在解锁

要突出或记笔记,您需要在订阅中拥有这本书或自行购买。