如何在 swift 中本地化我的多个字符串?

How can I localise my multiple string in swift?

当我这样做时,我可以用不同的语言编写所有的字符串表达式。但是当我制作多行字符串时它不会发生。请帮助我如何集成它?

模态

Unit(unit: "Unit 1".localized(),
             category: [Category(category: "Personal Pronouns".localized()])

控制器

extension String {
    func localized() -> String {
        return NSLocalizedString(self,
                                 tableName: "Localizable",
                                 bundle: .main,
                                 value: self,
                                 comment: self)
    }      
}

localizable.string

"Unit 1" = "Unité 1";
"Personal Pronouns" = "Pronoms personnel";

我想做的多行字符串是这样的,但是怎么做呢?

let text = """
    We want to change the World
    but not everywhere or everything
    only on people 
        """

https://github.com/ysrtirak/Make-localise 这是我的例子

我无法翻译“””“”中的文本

我没试过,但根据 this SO answer 你可以将多行字符串直接放入你的 localizable.strings 文件中:

localizable.strings:

"multiLine" = "We want to change the World
but not everywhere or everything
only on people";

并且在您的 swift 代码中:

print("multiLine".localized())

我刚刚尝试了一个测试应用程序,以下 localizable.strings 文件运行良好:

/* 
  Localizable.strings
  MultiLine

  Created by Duncan Champney on 8/13/21.
  
*/
"foo" = "This
is
a
multi-line
string";

对于 localizable.strings 文件,此代码:

        print(NSLocalizedString("foo", value: "foo", comment: "comment"))

打印输出:

This
is
a
multi-line
string

正如您所期望的那样。

编辑:

尝试从 Github 下载 this sample app。这是一个在 localizable.strings 文件中使用多行字符串的工作示例。

编辑#2:

您在法语中的英语开头和结尾多了一个换行符 localizable.strings。更改如下:

/* 
  Localizable.strings
  Make localise

  Created by Yasir Tırak on 15.08.2021.
  
*/

"I know how to localise that . thats not problem" = "Je sais comment localiser ça. ce n'est pas un problème";
"This is my text
But i cant localise it
hey
how are you" = "C'est mon texte
Mais je ne peux pas le localiser
Hé
Comment ça va";

有效。

为了弄清楚发生了什么,我将您的代码分解为多个步骤并记录了结果:

 let text = """
 This is my text
 But i cant localise it
 hey
 how are you
 """
 print("text = '\(text)'")
 let localizedText = text.localized()
 textLabel.text = localizedText

输出:

text = 'This is my text
But i cant localise it
hey
how are you'

请注意第一个单引号前没有换行符,最后一个单引号后 none。