Swift localisation, percentage sign escaping


(Adam Sutcliffe) #1

Hi,

I've been having an issue with a localised string, one of which has a
percentage sign in it:

"GAMERANK_5_DESC" = "Wow! You're racing ahead. Only 5%% of our users get
here!";

the key is built as such:

Obj-c : NSString *key = [NSString stringWithFormat:@"GAMERANK_%@_DESC"
,rank];

Swift: let key = "GAMERANK_\(rank)_DESC"

Then localised with the same macro: NSLocalizedString(key, @"");

The output is different though:

Obj- C = "Wow! You're racing ahead. Only 5% of our users get here!"
Swift = "Wow! You're racing ahead. Only 5%% of our users get here!"

Is this a bug in the swifts localisation parsing? Does the percentage sign
not need to be escaped in Swift?

Cheers

···

--


ADAM SUTCLIFFE
Software Engineer


+44 (0)7786 692 639
adam@peak.net <name@peak.net>
peak.net


(Saagar Jha) #2

Interpolation doesn’t need escaping, but String(format:) does.

Saagar Jha

···

On May 11, 2017, at 02:44, Adam Sutcliffe via swift-users <swift-users@swift.org> wrote:

Hi,

I've been having an issue with a localised string, one of which has a percentage sign in it:

"GAMERANK_5_DESC" = "Wow! You're racing ahead. Only 5%% of our users get here!";

the key is built as such:

Obj-c : NSString *key = [NSString stringWithFormat:@"GAMERANK_%@_DESC",rank];

Swift: let key = "GAMERANK_\(rank)_DESC"

Then localised with the same macro: NSLocalizedString(key, @"");

The output is different though:

Obj- C = "Wow! You're racing ahead. Only 5% of our users get here!"
Swift = "Wow! You're racing ahead. Only 5%% of our users get here!"

Is this a bug in the swifts localisation parsing? Does the percentage sign not need to be escaped in Swift?

Cheers

--


ADAM SUTCLIFFE
Software Engineer


+44 (0)7786 692 639 <>
adam@peak.net <mailto:name@peak.net>
peak.net <http://peak.net/>_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Zhao Xin) #3

You should try localizedStringWithFormat(_:_:slight_smile:
<https://developer.apple.com/reference/swift/string/1414192-localizedstringwithformat#>
.

`static func localizedStringWithFormat(_ format: String, _ arguments:
CVarArg...) -> String`

Zhaoxin

···

On Fri, May 12, 2017 at 1:42 AM, Saagar Jha via swift-users < swift-users@swift.org> wrote:

Interpolation doesn’t need escaping, but String(format:) does.

Saagar Jha

On May 11, 2017, at 02:44, Adam Sutcliffe via swift-users < > swift-users@swift.org> wrote:

Hi,

I've been having an issue with a localised string, one of which has a
percentage sign in it:

"GAMERANK_5_DESC" = "Wow! You're racing ahead. Only 5%% of our users get
here!";

the key is built as such:

Obj-c : NSString *key = [NSString stringWithFormat:@"GAMERANK_%@_DESC"
,rank];

Swift: let key = "GAMERANK_\(rank)_DESC"

Then localised with the same macro: NSLocalizedString(key, @"");

The output is different though:

Obj- C = "Wow! You're racing ahead. Only 5% of our users get here!"
Swift = "Wow! You're racing ahead. Only 5%% of our users get here!"

Is this a bug in the swifts localisation parsing? Does the percentage sign
not need to be escaped in Swift?

Cheers

--


ADAM SUTCLIFFE
Software Engineer


+44 (0)7786 692 639
adam@peak.net <name@peak.net>
peak.net
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


#4

Hi,

I've been having an issue with a localised string, one of which has a
percentage sign in it:

"GAMERANK_5_DESC" = "Wow! You're racing ahead. Only 5%% of our users get
here!";

the key is built as such:

Obj-c : NSString *key = [NSString stringWithFormat:@"GAMERANK_%@_DESC"
,rank];

Swift: let key = "GAMERANK_\(rank)_DESC"

Then localised with the same macro: NSLocalizedString(key, @"");

The output is different though:

Obj- C = "Wow! You're racing ahead. Only 5% of our users get here!"
Swift = "Wow! You're racing ahead. Only 5%% of our users get here!"

How do you check the output?
If you are using NSLog(output) in Obj-C and print(output) in Swift, that is
why.
The first parameter of NSLog is a format.

···

2017-05-11 18:44 GMT+09:00 Adam Sutcliffe via swift-users < swift-users@swift.org>:

Is this a bug in the swifts localisation parsing? Does the percentage sign
not need to be escaped in Swift?

Cheers

--


ADAM SUTCLIFFE
Software Engineer


+44 (0)7786 692 639
adam@peak.net <name@peak.net>
peak.net

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Zhao Xin) #5

Forgot to show an example.

In Swift, below code won't work,

let says = NSLocalizedString("It runs \(count) times", comment: "run times")

You should use

let says = String.localizedStringWithFormat(NSLocalizedString("It runs %@
times", comment: "run times"), String(count))

Zhaoxin

···

On Fri, May 12, 2017 at 2:23 AM, rintaro ishizaki via swift-users < swift-users@swift.org> wrote:

2017-05-11 18:44 GMT+09:00 Adam Sutcliffe via swift-users <
swift-users@swift.org>:

Hi,

I've been having an issue with a localised string, one of which has a
percentage sign in it:

"GAMERANK_5_DESC" = "Wow! You're racing ahead. Only 5%% of our users get
here!";

the key is built as such:

Obj-c : NSString *key = [NSString stringWithFormat:@"GAMERANK_%@_DESC"
,rank];

Swift: let key = "GAMERANK_\(rank)_DESC"

Then localised with the same macro: NSLocalizedString(key, @"");

The output is different though:

Obj- C = "Wow! You're racing ahead. Only 5% of our users get here!"
Swift = "Wow! You're racing ahead. Only 5%% of our users get here!"

How do you check the output?
If you are using NSLog(output) in Obj-C and print(output) in Swift, that
is why.
The first parameter of NSLog is a format.

Is this a bug in the swifts localisation parsing? Does the percentage
sign not need to be escaped in Swift?

Cheers

--


ADAM SUTCLIFFE
Software Engineer


+44 (0)7786 692 639
adam@peak.net <name@peak.net>
peak.net

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users