0

When I do as below, I can write all string expressions in different languages. But it doesn't happen when I make a multi line String. Please help me on how to Integrate this?

Modal

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

controller

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";

The multi line string I want to do will be like this but how ?

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

https://github.com/ysrtirak/Make-localise here there is my example

I could not translate the text in """ """

1 Answers1

2

I haven't tried it, but according to this SO answer you can put multi-line strings directly into your localizable.strings file:

localizable.strings:

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

And in your swift code:

print("multiLine".localized())

I just tried a test application, and the following localizable.strings file worked perfectly:

/* 
  Localizable.strings
  MultiLine

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

With that localizable.strings file, this code:

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

prints the output:

This
is
a
multi-line
string

Just as you would expect it to.

Edit:

Try downloading this sample app from Github. It is a working example of using multi-line strings in a localizable.strings file.

Edit #2:

You have an extra newline at the beginning and end of the English in your French localizable.strings. Change it as follows:

/* 
  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";

That works.

To figure out what was going on, I broke your code into steps and logged the results:

 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

That outputs:

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

Note how there are no newlines before the first single quote and none after the last single quote.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • No, I tried this, but Objective-c does not accept a multi-line text in this way, it gives an error. Even swift code does not accept multi-line for plain string expression in this way, it is necessary to put 3 quotation marks. – Yasir TIRAK Aug 14 '21 at 00:23
  • 1
    The strings file is not Objective-C. It's a very specific C-like syntax. – Duncan C Aug 14 '21 at 00:26
  • Yes, I understand, but the system definitely does not accept this. I tried and your solution is not working. – Yasir TIRAK Aug 14 '21 at 00:39
  • 1
    I just tested it with the localizable.strings file I added to my answer and it most definitely works. – Duncan C Aug 14 '21 at 00:40
  • 1
    See the edit to my answer. I just posted a link to a working project on Github that uses a multi-line localizable.strings file. – Duncan C Aug 14 '21 at 00:55
  • no you understood wrong sir, normally I have modal to put tableView for every section, Already I have multiple line texts with """text""" but when I want to localise it is not working because my string value is not "text" , it is already """text""" – Yasir TIRAK Aug 15 '21 at 10:58
  • Upload an example project with this problem to to github and add a link to it in your question. – Duncan C Aug 15 '21 at 14:35
  • I am able to include multi-line localized strings in a project's localizable.strings file. I'm not sure what you mean when you say that your string is "already """text"""." You can enclose a multi-line string literal in 3 pairs of double quotes, but it sounds like you might be mis-using that. Post a minimal project that shows this error and then we can help you sort it out. – Duncan C Aug 15 '21 at 17:05
  • I added an example please check it and give your idea. – Yasir TIRAK Aug 15 '21 at 18:40