SwiftUI 本地化 - 如何使 UI 组件及其功能成为本地化的基础
SwiftUI Localization - how to make the UI component and its function the base for localization
我遵循了关于如何本地化 iOS 应用程序的推荐方法——我使用 Base 本地化并将英语设置为开发语言。
所以我有这样的结构:
Localizable.strings
|___ Localizable.strings (English)
|___ Localizable.strings (German)
...
现在当我使用 Text("Cancel")
之类的东西时,我将在 Localizable.strings (German)
中得到以下内容:
/* No comment provided by engineer. */
"Cancel" = "Abbrechen";
然后我查看了 Facebook 在其捆绑包(Facebook Login
库)中使用的本地化文件,它们看起来像这样:
"ErrorRecovery.Cancel" = "Abbrechen";
知道当用户将应用程序语言切换到 English
(这是开发语言)时如何在不呈现 ErrorRecovery.Cancel
的情况下实现这一点吗?
这对我来说更理想,因为基础本身可以用作跨不同平台的 ID(例如,如果我还想本地化我的 Android 应用程序)。此外,有时一种语言中的同一个词并不总是翻译成另一种语言中的同一个词,例如Search
可能有以下含义:to search
和 the search engine
或 the process of searching
,在德语中可以翻译为 Suchen
和 Suche
对于后者。在那种情况下 Text("Search")
不足以涵盖这些情况。我知道我可以使用 NSLocalizedString("Search", comment: "verb")
和 NSLocalizedString("Search", comment: "the process of searching")
,但是本地化条目的 ID 将由 string
和 comment
.
组成
事实证明这很容易。我所要做的就是在 NSLocalizedString.init
.
中提供可选参数 value
NSLocalizedString
函数具有以下签名
/// - parameter key: An identifying value used to reference a localized string.
/// Don't use the empty string as a key. Values keyed by the empty string will
/// not be localized.
...
/// - parameter value: A user-visible string to return when the localized string
/// for `key` cannot be found in the table. If `value` is the empty string,
/// `key` would be returned instead.
NSLocalizedString(_ key: String, tableName: String? = nil, bundle: Bundle = Bundle.main, value: String = "", comment: String)
如果未提供 value
参数,则导出本地化后的翻译条目如下所示:
File.swift
NSLocalizedString("Cancel", comment: "Cancel button")
en.xliff
<trans-unit id="Cancel" xml:space="preserve">
<source>Cancel</source>
<target>Cancel</target>
<note>Cancel button</note>
</trans-unit>
但是,如果提供了 value
参数,生成的翻译条目如下所示:
File.swift
NSLocalizedString("Navigation.Button.Cancel", value: "Cancel", comment: "Cancel button")
en.xliff
<trans-unit id="Navigation.Button.Cancel" xml:space="preserve">
<source>Cancel</source>
<target>Cancel</target>
<note>Cancel button</note>
</trans-unit>
我遵循了关于如何本地化 iOS 应用程序的推荐方法——我使用 Base 本地化并将英语设置为开发语言。
所以我有这样的结构:
Localizable.strings
|___ Localizable.strings (English)
|___ Localizable.strings (German)
...
现在当我使用 Text("Cancel")
之类的东西时,我将在 Localizable.strings (German)
中得到以下内容:
/* No comment provided by engineer. */
"Cancel" = "Abbrechen";
然后我查看了 Facebook 在其捆绑包(Facebook Login
库)中使用的本地化文件,它们看起来像这样:
"ErrorRecovery.Cancel" = "Abbrechen";
知道当用户将应用程序语言切换到 English
(这是开发语言)时如何在不呈现 ErrorRecovery.Cancel
的情况下实现这一点吗?
这对我来说更理想,因为基础本身可以用作跨不同平台的 ID(例如,如果我还想本地化我的 Android 应用程序)。此外,有时一种语言中的同一个词并不总是翻译成另一种语言中的同一个词,例如Search
可能有以下含义:to search
和 the search engine
或 the process of searching
,在德语中可以翻译为 Suchen
和 Suche
对于后者。在那种情况下 Text("Search")
不足以涵盖这些情况。我知道我可以使用 NSLocalizedString("Search", comment: "verb")
和 NSLocalizedString("Search", comment: "the process of searching")
,但是本地化条目的 ID 将由 string
和 comment
.
事实证明这很容易。我所要做的就是在 NSLocalizedString.init
.
value
NSLocalizedString
函数具有以下签名
/// - parameter key: An identifying value used to reference a localized string.
/// Don't use the empty string as a key. Values keyed by the empty string will
/// not be localized.
...
/// - parameter value: A user-visible string to return when the localized string
/// for `key` cannot be found in the table. If `value` is the empty string,
/// `key` would be returned instead.
NSLocalizedString(_ key: String, tableName: String? = nil, bundle: Bundle = Bundle.main, value: String = "", comment: String)
如果未提供 value
参数,则导出本地化后的翻译条目如下所示:
File.swift
NSLocalizedString("Cancel", comment: "Cancel button")
en.xliff
<trans-unit id="Cancel" xml:space="preserve">
<source>Cancel</source>
<target>Cancel</target>
<note>Cancel button</note>
</trans-unit>
但是,如果提供了 value
参数,生成的翻译条目如下所示:
File.swift
NSLocalizedString("Navigation.Button.Cancel", value: "Cancel", comment: "Cancel button")
en.xliff
<trans-unit id="Navigation.Button.Cancel" xml:space="preserve">
<source>Cancel</source>
<target>Cancel</target>
<note>Cancel button</note>
</trans-unit>