Skip to content

Commit

Permalink
Merge pull request marmelroy#13 from rokoroku/master
Browse files Browse the repository at this point in the history
Adding localized with format function
  • Loading branch information
marmelroy committed Nov 23, 2015
2 parents 39d856d + 8dda294 commit f03f871
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
18 changes: 18 additions & 0 deletions Localize/Localize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public func Localized(string: String) -> String {
return string
}

/**
Swift 1.x friendly localization syntax with format arguments, replaces String(format:NSLocalizedString)
- Parameter string: Key to be localized.
- Returns: The formatted localized string with arguments.
*/
public func Localized(string: String, arguments args: CVarArgType...) -> String {
return String(format: Localized(string), arguments: args)
}

public extension String {
/**
Swift 2 friendly localization syntax, replaces NSLocalizedString
Expand All @@ -42,9 +51,18 @@ public extension String {
}
return self
}

/**
Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString)
- Returns: The formatted localized string with arguments.
*/
func localizedWithFormat(arguments args: CVarArgType...) -> String {
return String(format: localized(), arguments: args)
}
}



// MARK: Language Setting Functions

public class Localize: NSObject {
Expand Down
8 changes: 6 additions & 2 deletions genstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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\([^\n\r]*\)', re.DOTALL)

# get string list
uid = 0
Expand All @@ -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*"([^"]*)";')
Expand Down Expand Up @@ -142,4 +146,4 @@ def fetch_files_recursive(directory, extension):
else:
print '/* ' + string[1] + ' */'
print '"' + string[0] + '" = "' + string[0] + '";'
print
print

0 comments on commit f03f871

Please sign in to comment.