Skip to content

Commit

Permalink
Merge branch 'work/ignore_keys_in_code' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Jun 4, 2016
2 parents 5a2f028 + 590075d commit eccece2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ It is recommended that you update the `-p "$PROJECT_DIR"` appearances in this sc

---

### Exclude specific Views from Localization
### Exclude specific Views / NSLocalizedStrings from Localization

Sometimes you may want to **ignore some specific views** containing localizable texts e.g. because **their values are gonna be set programmatically**.
For these cases you can simply include `#bartycrouch-ignore!` or the shorthand `#bc-ignore!` into your value within your base localized Storyboard/XIB file.
Expand All @@ -293,6 +293,15 @@ Here's an example of how a base localized view in a XIB file with partly ignored

<img src="Exclusion-Example.png">

You can also use `#bc-ignore!` in your `NSLocalizedString` macros comment part to ignore them so they are not added to your `Localizable.strings`. This is helpful when you are using a `.stringsdict` file to handle pluralization (see [docs](https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/StringsdictFileFormat/StringsdictFileFormat.html)).

For example you can do something like this:
```swift
func updateTimeLabel(minutes: Int) {
String.localizedStringWithFormat(NSLocalizedString("%d minute(s) ago", comment: "pluralized and localized minutes #bc-ignore!"), minutes)
}
```
The `%d minute(s) ago` key will be taken from Localizable.stringsdict file, not from Localizable.strings.

## Migration Guides

Expand Down
9 changes: 7 additions & 2 deletions Sources/Code/StringsFileUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class StringsFileUpdater {

/// Updates the keys of this instances strings file with those of the given strings file.
/// Note that this will add new keys, remove not-existing keys but won't touch any existing ones.
public func incrementallyUpdateKeys(withStringsFileAtPath otherStringFilePath: String, addNewValuesAsEmpty: Bool, ignoreBaseKeys: [String] = ["#bartycrouch-ignore!", "#bc-ignore!", "#i!"], override: Bool = false, updateCommentWithBase: Bool = true, keepExistingKeys: Bool = false) {
public func incrementallyUpdateKeys(withStringsFileAtPath otherStringFilePath: String, addNewValuesAsEmpty: Bool, ignoreBaseKeysAndComment ignores: [String] = ["#bartycrouch-ignore!", "#bc-ignore!", "#i!"], override: Bool = false, updateCommentWithBase: Bool = true, keepExistingKeys: Bool = false) {

do {
let newContentString = try String(contentsOfFile: otherStringFilePath)
Expand Down Expand Up @@ -61,7 +61,12 @@ public class StringsFileUpdater {
for (key, newValue, newComment) in newTranslations {

// skip keys marked for ignore
guard !newValue.containsAny(ofStrings: ignoreBaseKeys) else {
guard !newValue.containsAny(ofStrings: ignores) else {
continue
}

// Skip keys that have been marked for ignore in comment
if let newComment = newComment where newComment.containsAny(ofStrings: ignores) {
continue
}

Expand Down
1 change: 1 addition & 0 deletions Tests/Assets/CodeFiles/Subfolder/SwiftExample2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class SwiftExample2 {

NSLocalizedString("TestKey2", comment: "Comment for TestKey1")
String(format: NSLocalizedString("%010d and %03.f", comment: ""), 25, 89.5)
String.localizableStringWithFormat(NSLocalizedString("%d ignore(s)", comment: "Ignoring stringsdict key #bc-ignore!"), 25)

}

Expand Down
3 changes: 3 additions & 0 deletions Tests/Code/GenStringsCommanderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class GenStringsCommanderTests: XCTestCase {
"/* No comment provided by engineer. */",
"\"%@ and %.2f\" = \"%1$@ and %2$.2f\";",
"",
"/* Ignoring stringsdict key #bc-ignore! */",
"\"%d ignore(s)\" = \"%d ignore(s)\";",
"",
"/* Comment for TestKey1 */",
"\"TestKey1\" = \"TestKey1\";",
"",
Expand Down

0 comments on commit eccece2

Please sign in to comment.