Swift string variables localization

2019-07-19 21:16发布

I have a big project with lots of text stored both in labels in storyboard and variables. I know how to localize text in labels, but have no idea how to localize string variables. Can you explain that or give a link to the guide. I'm using Xcode version 6.2

1条回答
仙女界的扛把子
2楼-- · 2019-07-19 22:11

In code, you use NSLocalizedString() with your base language:

NSLocalizedString("This is my text", comment: "Label in mainView")

The comment can be left empty "" but it is useful for the translator (even if this is you) to know where the string comes from.

Xcode automatically creates a file Localizable.strings in your project in a subfolder for the language. The file contains entries like that:

/* Label in mainView */
"This is my text" = "Dies ist mein Text";

You can translate the strings in that file with a text editor of your choice.

A better way to work with translations is to select the project target in Xcode, then choose from the menu

Editor->Export For Localization...

This will create a folder in a location of your choice containing a language file in xliff format, i.e. for german: de.xliff

xliff is a standardized (XML) format. It is somewhat hard to edit that file in a simple text editor. I use iXLIFF for that. It is a very small application for the Mac and makes translations a breeze.

After translating the xliff file go to Xcode

Editor->Import Localizations...

and choose the xliff file. Xcode will tell you when something is missing.

查看更多
登录 后发表回答