Convert NSLocalizedString to NSAttributedString uppercase the text

1.1k views Asked by At

Steps to reproduce:

let testString  = NSLocalizedString("test", comment: "string for test")
let testStringAttributed = NSAttributedString(string: testString)

print(testStringAttributed)

expected result : test

actual result : TEST

I am using 7.3.1, swift 2.2

You can see I didn't even set attributes, even with attributed (with a font style regular) the result is the same. This is an unexpected behaviour and I have to be able to localize my attributed strings in my app (basically a text I set in a label with links). Any known workaround?

P.S.: Setting the string to lowercase removes the uppercase BUT makes me lose any attributes I set. Not a viable option here.

1

There are 1 answers

0
thibaut noah On

The error was due to a missing translation like Timur Bernikowich suggested.

NSLocalizedString is supposed to return the key in the event of a missing translation but it appears in the case of a conversion to NSAttributedString the string gets uppercase.

Putting the proper translation in Localizable.string and calling the key fixed the problem