Genstrings throttles when using a parameter value in Swift

I have a main Swift Test.swift file containing

 import Foundation import UIKit class Test: NSObject { let a: String let b: String override init() { a = NSLocalizedString("key 1", tableName: nil, bundle: NSBundle.mainBundle(), value: "value 1", comment: "comment 1") b = NSLocalizedString("key 2", comment: "comment 2") } } 

When I run genstrings in this file, I get an unexpected warning

 $ genstrings -u Test.swift Bad entry in file Test.swift (line = 9): Argument is not a literal string. 

and in the generated Localizable.strings file there is no entry for "key 1"

 $ cat Localizable.strings ??/* comment 2 */ "key 2" = "key 2"; 

However, when I make an equivalent in Objective-C using the code below in the Test.m file

 #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface Test: NSObject @property (nonatomic, strong) NSString *a; @property (nonatomic, strong) NSString *b; @end @implementation Test - (id)init { self = [super init]; if (self) { self.a = NSLocalizedStringWithDefaultValue(@"key 1", nil, [NSBundle mainBundle], @"value 1", @"comment 1"); self.b = NSLocalizedString(@"key 2", @"comment 2"); } return self; } @end 

The genstrings command works as expected, and I get an entry for "key 1" .

 $ genstrings -u Test.m $ cat Localizable.strings ??/* comment 1 */ "key 1" = "value 1"; /* comment 2 */ "key 2" = "key 2"; 

What am I doing wrong?

+6
source share
2 answers

This is a generation error in Xcode 6.4 and Xcode 7 beta, as in https://openradar.appspot.com/22133811 :

In Swift files, genstrings Chokes On NSLocalizedString calls more than two parameters

Summary: when you run genstrings against a Swift file, if there are any calls to NSLocalizedString that use more than the trivial case the parameters "value" and "comment" generate errors ....

+10
source

Obviously, Apple has stepped back from supporting genres. Use instead:

 xcrun extractLocStrings 

as your team. For example, to create Localizable.strings for your project:

 find ./ -name "*.m" -print0 | xargs -0 xcrun extractLocStrings -o en.lproj 

and for Swift:

 find ./ -name "*.swift" -print0 | xargs -0 xcrun extractLocStrings -o en.lproj 

Note that if you intend to export to a .xliff file, you no longer need to run genstrings as xCode

Editor> Export for Localization

Command

will process your lines "behind the scenes".

Update: Im on xCode 7.3.1 and on my system xtractLocStrings is binary.

 $ file /Applications/Xcode.app//Contents/Developer/usr/bin/extractLocStrings /Applications/Xcode.app//Contents/Developer/usr/bin/extractLocStrings: Mach-O 64-bit executable x86_64 

Here is my test:

 let _ = NSLocalizedString("1st", comment: "1st string") let _ = NSLocalizedString("Second", tableName: "Localized", bundle: NSBundle.mainBundle(), value: "2nd", comment: "2nd string") 

and here are the results:

 Localizable.strings: /* 1st string */ "1st" = "1st"; Localized.strings: /* 2nd string */ "Second" = "2nd"; 
+14
source

All Articles