Skip to content

Instantly share code, notes, and snippets.

@bmoliveira
Last active May 10, 2016 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmoliveira/7c5c980298f23887c555 to your computer and use it in GitHub Desktop.
Save bmoliveira/7c5c980298f23887c555 to your computer and use it in GitHub Desktop.
String Localization extension
public extension String {
/**
Swift 2 friendly localization syntax, replaces NSLocalizedString
- Returns: The localized string.
*/
func localized() -> String {
if let path = NSBundle.mainBundle().pathForResource(Localize.currentLanguage(), ofType: "lproj"), bundle = NSBundle(path: path) {
return bundle.localizedStringForKey(self, value: nil, table: nil)
}
else if let path = NSBundle.mainBundle().pathForResource(LCLBaseBundle, ofType: "lproj"), bundle = NSBundle(path: path) {
return bundle.localizedStringForKey(self, value: nil, table: nil)
}
return self
}
/**
Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString)
- Returns: The formatted localized string with arguments.
*/
func localizedFormat(arguments: CVarArgType...) -> String {
return String(format: localized(), arguments: arguments)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment