Skip to content

Commit

Permalink
Merge pull request #10 from nextbike/bug/emoji_unsupported
Browse files Browse the repository at this point in the history
Emoji (UTF-16) Support
  • Loading branch information
jonasvogel authored Jan 23, 2024
2 parents b311219 + b0e456a commit e9b95d6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
2 changes: 1 addition & 1 deletion PhraseSwift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Pod::Spec.new do |s|
s.name = 'PhraseSwift'
s.version = '1.0.0'
s.version = '1.0.1'
s.summary = 'A swift port of Squares Phrase library.'

s.description = <<-DESC
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ In Project Settings, on the tab "Package Dependencies", click "+" and add `githu
1. Add a dependency in Package.swift:
```swift
dependencies: [
.package(url: "https://github.com/nextbike/phrase-swift", from: "1.0.0")
.package(url: "https://github.com/nextbike/phrase-swift", from: "1.0.1")
]
```
2. For each relevant target, add a dependency
Expand All @@ -48,7 +48,7 @@ Also check out [Editing a package dependency as a local package](https://develop
[CocoaPods](https://cocoapods.org/) is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website.

```
pod 'PhraseSwift', :git => 'https://github.com/nextbike/phrase-swift.git', '~> 1.0.0
pod 'PhraseSwift', :git => 'https://github.com/nextbike/phrase-swift.git', '~> 1.0.1
```

## Usage
Expand Down

This file was deleted.

29 changes: 22 additions & 7 deletions Sources/PhraseSwift/Phrase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class Phrase {
// indicates parsing is complete

// TODO: make it int 1
public static let EOF: Character = Character(UnicodeScalar(0))
public static let EOF: Character = Character(Unicode.Scalar(0))

@discardableResult public func put(key: String, value: String) -> Phrase {
guard keys.contains(key) else {
Expand Down Expand Up @@ -243,8 +243,16 @@ public final class Phrase {
}

override func expand(target: NSMutableAttributedString ,data: [String: String] ) {
let start = getFormattedStart()
target.replace(start: start, end: start + 2, toReplaceWith: "\(PhraseConfig.shared.keyStartChar)")
let replaceFrom = target.string.index(target.string.startIndex, offsetBy: getFormattedStart())
let replaceTo = target.string.index(replaceFrom, offsetBy: 1)


let expanded = target.string.replacingCharacters(
in: replaceFrom...replaceTo,
with: "\(PhraseConfig.shared.keyStartChar)"
)

target.setAttributedString(NSAttributedString(string: expanded))
}

override func getFormattedLength() -> Int {
Expand All @@ -253,7 +261,7 @@ public final class Phrase {
}
}

private class KeyToken : Token {
private class KeyToken : Token {
/** The key without { and }. */
private var key : String
private var value : String?
Expand All @@ -265,12 +273,19 @@ public final class Phrase {

override func expand( target: NSMutableAttributedString, data: [String: String]) {
value = data[key]
let replaceFrom = getFormattedStart()
guard let value else { return }

let replaceFrom = target.string.index(target.string.startIndex, offsetBy: getFormattedStart())

// Add 2 to account for the opening and closing brackets.
let replaceTo = replaceFrom + key.count + 2
let replaceTo = target.string.index(
target.string.startIndex,
offsetBy: getFormattedStart() + key.count + 2
)

let expanded = target.string.replacingCharacters(in: replaceFrom..<replaceTo, with: value)

target.replace(start: replaceFrom, end: replaceTo, toReplaceWith: value)
target.setAttributedString(NSAttributedString(string: expanded))
}

override func getFormattedLength() -> Int {
Expand Down
18 changes: 18 additions & 0 deletions Tests/PhraseSwiftTests/PhraseSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,22 @@ class PhraseSwiftTests: XCTestCase {
"key_one": "bidirectional"])
XCTAssert(secondOutput == "Hi, this is a bidirectional test with multiple keys.")
}

func testUserGreeting() {

XCTAssertEqual(Phrase.localize(
"👋 Hey {username} !",
keyValues: ["username": "Rider"]
), "👋 Hey Rider !")

XCTAssertEqual(Phrase.localize(
"Hey {username}! 👋",
keyValues: ["username": "Rider"]
), "Hey Rider! 👋")

XCTAssertEqual(Phrase.localize(
"Hey {username} 👋!",
keyValues: ["username": "Rider"]
), "Hey Rider 👋!")
}
}

0 comments on commit e9b95d6

Please sign in to comment.