forked from marmelroy/Localize-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
84 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// String+LocalizeTableName.swift | ||
// Localize_Swift | ||
// | ||
// Created by Vitalii Budnik on 3/10/16. | ||
// Copyright © 2016 Vitalii Budnik. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// tableName friendly extension | ||
public extension String { | ||
|
||
/** | ||
Swift 2 friendly localization syntax, replaces NSLocalizedString | ||
|
||
- Parameter tableName: The receiver’s string table to search. If tableName is `nil` | ||
or is an empty string, the method attempts to use `Localizable.strings`. | ||
|
||
- Returns: The localized string. | ||
*/ | ||
func localized(using tableName: String?) -> String { | ||
if let path = Bundle.main.path(forResource: Localize.currentLanguage(), ofType: "lproj"), | ||
let bundle = Bundle(path: path) { | ||
return bundle.localizedString(forKey: self, value: nil, table: tableName) | ||
} | ||
else if let path = Bundle.main.path(forResource: LCLBaseBundle, ofType: "lproj"), | ||
let bundle = Bundle(path: path) { | ||
return bundle.localizedString(forKey: self, value: nil, table: tableName) | ||
} | ||
return self | ||
} | ||
|
||
/** | ||
Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString) | ||
|
||
- Parameter tableName: The receiver’s string table to search. If tableName is `nil` | ||
or is an empty string, the method attempts to use `Localizable.strings`. | ||
|
||
- Parameter arguments: arguments values for temlpate (substituted according to the user’s default locale). | ||
|
||
- Returns: The formatted localized string with arguments. | ||
*/ | ||
func localizedFormat(using tableName: String?, arguments: CVarArg...) -> String { | ||
return String(format: localized(using: tableName), arguments: arguments) | ||
} | ||
|
||
/** | ||
Swift 2 friendly plural localization syntax with a format argument | ||
|
||
- Parameter tableName: The receiver’s string table to search. If tableName is `nil` | ||
or is an empty string, the method attempts to use `Localizable.strings`. | ||
|
||
- parameter argument: Argument to determine pluralisation | ||
|
||
- returns: Pluralized localized string. | ||
*/ | ||
func localizedPlural(using tableName: String?, argument: CVarArg) -> String { | ||
return NSString.localizedStringWithFormat(localized(using: tableName) as NSString, argument) as String | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
examples/LanguageSwitch/Sample/fr.lproj/ButtonTitles.strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
ButtonTitles.strings | ||
Sample | ||
|
||
Created by Vitalii Budnik on 10/6/16. | ||
Copyright © 2016 Roy Marmelstein. All rights reserved. | ||
*/ | ||
|
||
"Change" = "Modifier"; | ||
|
||
"Reset" = "Réinitialiser"; |