ios 如何本地化 Images.xcassets 中的图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21310819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:13:58  来源:igfitidea点击:

How to localize the images in Images.xcassets?

ioslocalization

提问by SamirChen

We can localize an image in the File Inspectorusing Localize...like this:

我们可以在File Inspector使用中定位图像,Localize...如下所示:

enter image description here

在此处输入图片说明

Then we can get this:

然后我们可以得到这个:

enter image description here

在此处输入图片说明

But now, I use Images.xcassets to manage my images in my iOS project, how should I localize these images in Images.xcassets?

但是现在,我使用 Images.xcassets 来管理我的 iOS 项目中的图像,我应该如何在 Images.xcassets 中本地化这些图像?

回答by cc.

If you are using an Asset Catalog:

如果您使用的是资产目录:

Asset catalog elements are now localizable. In the information panel for an asset, there is a "Localization" section that lists all the languages you've set up for your project in the project settings. If you don't select any of them, your images will be deemed "universal" (i.e., they will adopt the default behavior). If you select one or more of them, you will be able to select different images for "universal" and any alternate language.

资产目录元素现在可本地化。在资产的信息面板中,有一个“本地化”部分,其中列出了您在项目设置中为项目设置的所有语言。如果您不选择其中任何一个,您的图像将被视为“通用”(即,它们将采用默认行为)。如果您选择其中一个或多个,您将能够为“通用”和任何替代语言选择不同的图像。

For example, if your base language is English, and you want Spanish-language alternates, select only "Spanish" and drag in the alternate versions in the Spanish wells, and leave the English versions as the Universal. Then, at run-time, if the chosen language is Spanish, then the Spanish-language image will be pulled. Otherwise, it will pull the English version. (Or, if you want specific English and Spanish versions that are both different from "everything else", also check English and drag in the corresponding English and Universal images.)

例如,如果您的基本语言是英语,而您想要西班牙语替代语言,则仅选择“西班牙语”并在西班牙语井中拖入替代版本,并将英语版本保留为通用版本。然后,在运行时,如果选择的语言是西班牙语,则将拉取西班牙语图像。否则,它将拉出英文版本。(或者,如果您想要与“其他所有内容”都不同的特定英语和西班牙语版本,还可以选中英语并拖入相应的英语和通用图像。)

If you need to determine localized images at run time without using the Asset Catalog:

如果您需要在运行时确定本地化图像而不使用资产目录:

While there's (apparently) notcurrently a way to explicitly localize the xcassets file directly, you could instead localize the name of the asset to pull using your Localizable.strings file. For instance, say you have a graphic logo treatment for the main menu of a game that needs to be localized. You could put the localized logo treatments in the assets file with different names, add the names to your Localizable.strings file, and then fetch the appropriate image with code like this:

虽然(显然)目前没有一种方法可以直接显式本地化 xcassets 文件,但您可以使用 Localizable.strings 文件本地化要提取的资产名称。例如,假设您对需要本地化的游戏主菜单进行了图形徽标处理。您可以将本地化的徽标处理放在具有不同名称的资产文件中,将名称添加到 Localizable.strings 文件中,然后使用如下代码获取适当的图像:

UIImage *img = [UIImage imageNamed:NSLocalizedString(@"MAIN_MENU_IMAGE", nil)];

回答by CodeBender

When Apple gives you lemons, make lemonade, or in this case, lemonade_en, lemonade_es or whatever suits your needs.

当 Apple 给你柠檬时,制作柠檬水,或者在这种情况下,lemonade_en、lemonade_es 或任何适合你需要的东西。

First, I create an entry for each desired language in my assets file like so:

首先,我在资产文件中为每种所需的语言创建一个条目,如下所示:

enter image description here

在此处输入图片说明

Next, you will need to get a language code for the device. The important thing to remember here is that the user may have an unsupported language, so if that is the case, return your default (I use English).

接下来,您需要获取设备的语言代码。这里要记住的重要一点是用户可能使用不受支持的语言,因此如果是这种情况,请返回您的默认值(我使用英语)。

The following extension of UIApplicationwill handle all of this for you:

以下扩展UIApplication名将为您处理所有这些:

extension UIApplication {
    var languageCode: String {
        let supportedLanguageCodes = ["en", "es", "fr"]
        let languageCode = Locale.current.languageCode ?? "en"

        return supportedLanguageCodes.contains(languageCode) ? languageCode : "en"
    }
}

This extension does the following:

此扩展执行以下操作:

  • Creates an array of the languages my app supports
  • Obtains the current device's language code, or returns a default value if this is nil
  • Finally, it checks to see if the current code is in my supported list. If it is, it returns it, otherwise it returns my default code
  • 创建我的应用支持的语言数组
  • 获取当前设备的语言代码,如果为 nil 则返回默认值
  • 最后,它会检查当前代码是否在我支持的列表中。如果是,它返回它,否则它返回我的默认代码

Now, we combine the two to get the proper image:

现在,我们将两者结合起来以获得正确的图像:

let languageCode = UIApplication.shared.languageCode
let image = UIImage(named: "access_\(languageCode)")

回答by johnMa

After some search on the Internet, I think this feature is not provide by xcassets right now. Therefore, just don't use xcassets to manage your localization images.

在网上搜索了一些,我认为这个功能现在不是由 xcassets 提供的。因此,不要使用 xcassets 来管理您的本地化图像。

回答by Vladimir Grigorov

That "Localize..." button is back in Xcode 11! So now you can localize your images and assets in the Asset catalog as you expect:

“本地化...”按钮又回到了 Xcode 11 中!因此,现在您可以按预期在 Asset 目录中本地化您的图像和资产:

enter image description here

在此处输入图片说明

回答by Ugo Chirico

I found another way:

我找到了另一种方法:

  1. add in you asset.xcassets a folder for each language (for instance: en,it,de)
  2. set the flag "Provides Namespace" to each folder
  3. copy all images and assets in that folder
  4. In your code load the assets by calling
  1. 在您的 asset.xcassets 中为每种语言添加一个文件夹(例如:en、it、de)
  2. 为每个文件夹设置标志“提供命名空间”
  3. 复制该文件夹中的所有图像和资产
  4. 在您的代码中,通过调用加载资产
let languageCode = UIApplication.shared.languageCode
let image = UIImage(named: "\(languageCode)\assetname")
let languageCode = UIApplication.shared.languageCode
let image = UIImage(named: "\(languageCode)\assetname")

回答by ninjaneer

At the moment, I pull out the images that needs localization from Images.xcassets and put it in the standard project folder. I leave the non-localized images in the .xcassets. I use your method for the images that need to be localized.

目前,我从Images.xcassets中取出需要本地化的图片,放到标准项目文件夹中。我将非本地化图像留在 .xcassets 中。我将您的方法用于需要本地化的图像。