如何制作支持不同语言的iOS应用。

huangapple go评论66阅读模式
英文:

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

你需要创建本地化文件的步骤如下:

  1. 转到项目导航。
  2. 单击项目。
  3. 在“信息”选项卡中向下滚动以查看“本地化”部分,在该部分中,您可以选择您的应用程序要支持的语言。
  4. 点击加号图标并选择语言,然后继续。
  5. 现在,从导航窗格底部出现的加号图标创建字符串文件,文件名必须为“Localisation”。
  6. 创建新文件后,它将如下所示。
  7. 现在,您将在右侧窗格中看到“本地化”按钮。单击该按钮并继续。
  8. 您可以从列表中选择任何语言。
  9. 现在,您的右侧窗格将如下所示。
  10. 现在选择要包含本地化内容的所有文件。
  11. 您可以按以下方式在这些文件中添加翻译:

"sign" = "Sign";
"continue" = "Continue";

并在印地语本地化文件中添加以下代码:

"sign" = "साइन इन करें";
"continue" = "जारी रखें";

以及其他每个本地化文件中的类似内容与其翻译。

  1. 在您的代码中添加以下扩展:
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: "")
  }

}
  1. 现在,您可以使用此扩展来显示本地化内容,如下所示:
let langCode = "en"
yourLbl.text = "sign".localizeString(string: langCode)
  1. 您需要更改langCode的值,并将该值传递给localizeString,然后根据该文件更改翻译。
英文:

you have to create the localisation file steps are :

  1. Go to project Navigations
  2. Click on the project
  3. 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.
    如何制作支持不同语言的iOS应用。
  4. Press the plus icon and select the language and continue.
  5. Now create the string File from the plus icon appearing at the bottom in the navigation pane and the file name must be Localisation
  6. When the new file is created it will look Like the below Screenshot.
    如何制作支持不同语言的iOS应用。
  7. Now you will see the localise button in the right side pane. click on that button and continue
  8. You can select any of the languages from the list.
  9. Now you will see your right pane look like below.
    如何制作支持不同语言的iOS应用。
  10. Now select all the files that you want to have the localisation content.
  11. 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

  1. 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: "")
  }

}
  1. Now you can use this extension to display the localised content as
    below.
let langCode = "en"
yourLbl.text = "sign".localizeString(string: langCode)
  1. You have to change the value langCode and pass that value to the localizeString and change the translation according to that file.

huangapple
  • 本文由 发表于 2023年7月3日 13:40:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定