From 4e8a8db3c5ef04ba7ef155972c4d27f6f42f1f26 Mon Sep 17 00:00:00 2001 From: rokoroku Date: Mon, 23 Nov 2015 14:33:27 +0900 Subject: [PATCH 1/2] Adding localized with format function --- Localize/Localize.swift | 13 ++++++++++++- genstrings.py | 8 ++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Localize/Localize.swift b/Localize/Localize.swift index 556e1a8..4067be3 100644 --- a/Localize/Localize.swift +++ b/Localize/Localize.swift @@ -23,17 +23,28 @@ public func Localized(string: String) -> String { return string! } -// Swift 2 friendly localization syntax, replaces NSLocalizedString +// Swift 1.x friendly localization syntax with format arguments, replaces String(format:NSLocalizedString) +public func Localized(string: String, args: CVarArgType...) -> String { + return String(format: Localized(string), arguments: args) +} + public extension String { + // Swift 2 friendly localization syntax, replaces NSLocalizedString func localized() -> String { let path = NSBundle.mainBundle().pathForResource(Localize.currentLanguage(), ofType: "lproj") let bundle = NSBundle(path: path!) let string = bundle?.localizedStringForKey(self, value: nil, table: nil) return string! } + + // Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString) + func localizedWithFormat(args: CVarArgType...) -> String { + return String(format: localized(), arguments: args) + } } + // MARK: Language Setting Functions public class Localize: NSObject { diff --git a/genstrings.py b/genstrings.py index bd1e648..8d078e2 100644 --- a/genstrings.py +++ b/genstrings.py @@ -40,8 +40,9 @@ def fetch_files_recursive(directory, extension): # prepare regexes localizedStringComment = re.compile('NSLocalizedString\("([^"]*)",\s*"([^"]*)"\s*\)', re.DOTALL) localizedStringNil = re.compile('NSLocalizedString\("([^"]*)",\s*nil\s*\)', re.DOTALL) -localized = re.compile('Localized\("([^"]*)"\)', re.DOTALL) +localized = re.compile('Localized\("([^"]*)"[^\n\r]*\)', re.DOTALL) localizedSwift2 = re.compile('"([^"]*)".localized\(\)', re.DOTALL) +localizedSwift2WithFormat = re.compile('"([^"]*)".localizedWithFormat\(.*\)', re.DOTALL) # get string list uid = 0 @@ -61,6 +62,9 @@ def fetch_files_recursive(directory, extension): for result in localizedSwift2.finditer(content): uid += 1 strings.append((result.group(1), '', file, uid)) + for result in localizedSwift2WithFormat.finditer(content): + uid += 1 + strings.append((result.group(1), '', file, uid)) # prepare regexes localizedString = re.compile('"[^=]*=\s*"([^"]*)";') @@ -142,4 +146,4 @@ def fetch_files_recursive(directory, extension): else: print '/* ' + string[1] + ' */' print '"' + string[0] + '" = "' + string[0] + '";' - print + print \ No newline at end of file From 9c03843be1ecb5b841f342ba44b11dd6936e9ecd Mon Sep 17 00:00:00 2001 From: rokoroku Date: Mon, 23 Nov 2015 14:41:06 +0900 Subject: [PATCH 2/2] Fix the genstrings.py regex conversion --- genstrings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/genstrings.py b/genstrings.py index 8d078e2..75c9f10 100644 --- a/genstrings.py +++ b/genstrings.py @@ -42,7 +42,7 @@ def fetch_files_recursive(directory, extension): localizedStringNil = re.compile('NSLocalizedString\("([^"]*)",\s*nil\s*\)', re.DOTALL) localized = re.compile('Localized\("([^"]*)"[^\n\r]*\)', re.DOTALL) localizedSwift2 = re.compile('"([^"]*)".localized\(\)', re.DOTALL) -localizedSwift2WithFormat = re.compile('"([^"]*)".localizedWithFormat\(.*\)', re.DOTALL) +localizedSwift2WithFormat = re.compile('"([^"]*)".localizedWithFormat\([^\n\r]*\)', re.DOTALL) # get string list uid = 0