英文:
How to make iOS app that support different languages
问题
我必须创建一个应用程序,该应用程序可以在不同的语言中工作,并显示我从语言列表中选择的不同语言的应用内容。我有三种语言的列表:1. 英语,2. 印地语,3. 西班牙语。
制作应用程序支持多种语言的步骤是什么?目前,我的应用程序只能使用英语。
我期望在选择不同语言后,我的应用程序的整个文本都会转换为所选择的语言。
英文:
I have to create an app Which will work in different languages and show the content of app in different languages that i select from the language list. I have list of three languages :- 1. English , 2. Hindi and 3. Spanish .
What are the steps to make the app support multiple languages . right now my app is working only with english language.
I'm expecting as i change select a different language the whole text of my app get converted into the selected language.
答案1
得分: 1
你需要创建本地化文件的步骤如下:
- 转到项目导航。
- 单击项目。
- 在“信息”选项卡中向下滚动以查看“本地化”部分,在该部分中,您可以选择您的应用程序要支持的语言。
- 点击加号图标并选择语言,然后继续。
- 现在,从导航窗格底部出现的加号图标创建字符串文件,文件名必须为“Localisation”。
- 创建新文件后,它将如下所示。
- 现在,您将在右侧窗格中看到“本地化”按钮。单击该按钮并继续。
- 您可以从列表中选择任何语言。
- 现在,您的右侧窗格将如下所示。
- 现在选择要包含本地化内容的所有文件。
- 您可以按以下方式在这些文件中添加翻译:
"sign" = "Sign";
"continue" = "Continue";
并在印地语本地化文件中添加以下代码:
"sign" = "साइन इन करें";
"continue" = "जारी रखें";
以及其他每个本地化文件中的类似内容与其翻译。
- 在您的代码中添加以下扩展:
extension String {
func localizeString(string: String) -> String {
let path = Bundle.main.path(forResource: string, ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(self, tableName: nil, bundle: bundle!,
value: "", comment: "")
}
}
- 现在,您可以使用此扩展来显示本地化内容,如下所示:
let langCode = "en"
yourLbl.text = "sign".localizeString(string: langCode)
- 您需要更改langCode的值,并将该值传递给localizeString,然后根据该文件更改翻译。
英文:
you have to create the localisation file steps are :
- Go to project Navigations
- Click on the project
- In the Info tab scroll down to see the Localisation section, in that section
you can select the language that you want your app to support.
- Press the plus icon and select the language and continue.
- Now create the string File from the plus icon appearing at the bottom in the navigation pane and the file name must be Localisation
- When the new file is created it will look Like the below Screenshot.
- Now you will see the localise button in the right side pane. click on that button and continue
- You can select any of the languages from the list.
- Now you will see your right pane look like below.
- Now select all the files that you want to have the localisation content.
- You can add the translation in these files with the syntax like below:
"sign" = "Sign";
"continue" = "Continue";
and below code in the Hindi localisation file
"sign" = "साइन इन करें";
"continue" = "जारी रखें";
and similar content in each localisation file with their translation
- Add the below extension to your code :
extension String {
func localizeString(string: String) -> String {
let path = Bundle.main.path(forResource: string, ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(self, tableName: nil, bundle: bundle!,
value: "", comment: "")
}
}
- Now you can use this extension to display the localised content as
below.
let langCode = "en"
yourLbl.text = "sign".localizeString(string: langCode)
- You have to change the value langCode and pass that value to the localizeString and change the translation according to that file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论