Fix localization issues, add tests, import translations, and add zh-CN and zh-TW

Closes: #689
This commit is contained in:
2023-02-24 12:26:43 -05:00
committed by William Casarin
parent 85e797a054
commit 5cd4c2d75e
73 changed files with 1061 additions and 600 deletions

View File

@@ -0,0 +1,56 @@
//
// EventDetailBarTests.swift
// damusTests
//
// Created by Terry Yiu on 2/24/23.
//
import XCTest
@testable import damus
final class EventDetailBarTests: XCTestCase {
let enUsLocale = Locale(identifier: "en-US")
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testRepostsCountString() throws {
XCTAssertEqual(repostsCountString(0, locale: enUsLocale), "Reposts")
XCTAssertEqual(repostsCountString(1, locale: enUsLocale), "Repost")
XCTAssertEqual(repostsCountString(2, locale: enUsLocale), "Reposts")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
for count in 1...10 {
XCTAssertNoThrow(repostsCountString(count, locale: $0))
}
}
}
func testReactionsCountString() throws {
XCTAssertEqual(reactionsCountString(0, locale: enUsLocale), "Reactions")
XCTAssertEqual(reactionsCountString(1, locale: enUsLocale), "Reaction")
XCTAssertEqual(reactionsCountString(2, locale: enUsLocale), "Reactions")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
for count in 1...10 {
XCTAssertNoThrow(reactionsCountString(count, locale: $0))
}
}
}
func testZapssCountString() throws {
XCTAssertEqual(zapsCountString(0, locale: enUsLocale), "Zaps")
XCTAssertEqual(zapsCountString(1, locale: enUsLocale), "Zap")
XCTAssertEqual(zapsCountString(2, locale: enUsLocale), "Zaps")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
for count in 1...10 {
XCTAssertNoThrow(zapsCountString(count, locale: $0))
}
}
}
}

View File

@@ -34,4 +34,23 @@ final class FormatTests: XCTestCase {
XCTAssertEqual(format_msats_abbrev(1000), "1")
}
func testFormatMsats() throws {
let enUsLocale = Locale(identifier: "en-US")
XCTAssertEqual(format_msats(0, locale: enUsLocale), "0 sats")
XCTAssertEqual(format_msats(1, locale: enUsLocale), "0.001 sats")
XCTAssertEqual(format_msats(1000, locale: enUsLocale), "1 sat")
XCTAssertEqual(format_msats(1001, locale: enUsLocale), "1.001 sats")
XCTAssertEqual(format_msats(2000, locale: enUsLocale), "2 sats")
XCTAssertEqual(format_msats(123456789, locale: enUsLocale), "123,456.789 sats")
// Sanity check that function call does not throw in any supported locale as the string format accepts arguments, and a mismatched format in any one of the locales could break the app.
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
XCTAssertNoThrow(format_msats(0, locale: $0))
XCTAssertNoThrow(format_msats(1, locale: $0))
XCTAssertNoThrow(format_msats(1000, locale: $0))
XCTAssertNoThrow(format_msats(1001, locale: $0))
XCTAssertNoThrow(format_msats(2000, locale: $0))
XCTAssertNoThrow(format_msats(123456789, locale: $0))
}
}
}

View File

@@ -0,0 +1,45 @@
//
// ProfileViewTests.swift
// damusTests
//
// Created by Terry Yiu on 2/24/23.
//
import XCTest
@testable import damus
final class ProfileViewTests: XCTestCase {
let enUsLocale = Locale(identifier: "en-US")
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testFollowersCountString() throws {
XCTAssertEqual(followersCountString(0, locale: enUsLocale), "Followers")
XCTAssertEqual(followersCountString(1, locale: enUsLocale), "Follower")
XCTAssertEqual(followersCountString(2, locale: enUsLocale), "Followers")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
for count in 1...10 {
XCTAssertNoThrow(followersCountString(count, locale: $0))
}
}
}
func testRelaysCountString() throws {
XCTAssertEqual(relaysCountString(0, locale: enUsLocale), "Relays")
XCTAssertEqual(relaysCountString(1, locale: enUsLocale), "Relay")
XCTAssertEqual(relaysCountString(2, locale: enUsLocale), "Relays")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
for count in 1...10 {
XCTAssertNoThrow(relaysCountString(count, locale: $0))
}
}
}
}

View File

@@ -0,0 +1,87 @@
//
// ReplyDescriptionTests.swift
// damusTests
//
// Created by Terry Yiu on 2/21/23.
//
import XCTest
@testable import damus
final class ReplyDescriptionTests: XCTestCase {
let enUsLocale = Locale(identifier: "en-US")
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
// Test that English strings work properly with argument substitution and pluralization, and that other locales don't crash.
func testReplyDesc() throws {
let profiles = test_damus_state().profiles
let replyingToSelfEvent = test_event
XCTAssertEqual(reply_desc(profiles: profiles, event: replyingToSelfEvent, locale: enUsLocale), "Replying to self")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
XCTAssertNoThrow(reply_desc(profiles: profiles, event: replyingToSelfEvent, locale: $0))
}
let replyingToOne = NostrEvent(
content: "hello there https://jb55.com/s/Oct12-150217.png https://jb55.com/red-me.jpg cool",
pubkey: "pk",
tags: [["e", "123"], ["p", "123"]],
createdAt: Int64(Date().timeIntervalSince1970 - 100)
)
XCTAssertEqual(reply_desc(profiles: profiles, event: replyingToOne, locale: enUsLocale), "Replying to \(Profile.displayName(profile: nil, pubkey: "123"))")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
XCTAssertNoThrow(reply_desc(profiles: profiles, event: replyingToOne, locale: $0))
}
let replyingToTwo = NostrEvent(
content: "hello there https://jb55.com/s/Oct12-150217.png https://jb55.com/red-me.jpg cool",
pubkey: "pk",
tags: [["e", "123"], ["p", "123"], ["p", "456"]],
createdAt: Int64(Date().timeIntervalSince1970 - 100)
)
XCTAssertEqual(reply_desc(profiles: profiles, event: replyingToTwo, locale: enUsLocale), "Replying to \(Profile.displayName(profile: nil, pubkey: "456")) & \(Profile.displayName(profile: nil, pubkey: "123"))")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
XCTAssertNoThrow(reply_desc(profiles: profiles, event: replyingToTwo, locale: $0))
}
let replyingToTwoAndOneOther = NostrEvent(
content: "hello there https://jb55.com/s/Oct12-150217.png https://jb55.com/red-me.jpg cool",
pubkey: "pk",
tags: [["e", "123"], ["p", "123"], ["p", "456"], ["p", "789"]],
createdAt: Int64(Date().timeIntervalSince1970 - 100)
)
XCTAssertEqual(reply_desc(profiles: profiles, event: replyingToTwoAndOneOther, locale: enUsLocale), "Replying to \(Profile.displayName(profile: nil, pubkey: "789")), \(Profile.displayName(profile: nil, pubkey: "456")) & 1 other")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
XCTAssertNoThrow(reply_desc(profiles: profiles, event: replyingToTwoAndOneOther, locale: $0))
}
for othersCount in 2...10 {
var tags: [[String]] = [["e", "123"]]
for i in 1...othersCount {
tags.append(["p", "\(i)"])
}
tags.append(["p", "456"])
tags.append(["p", "789"])
let replyingToTwoAndMultipleOthers = NostrEvent(
content: "hello there https://jb55.com/s/Oct12-150217.png https://jb55.com/red-me.jpg cool",
pubkey: "pk",
tags: tags,
createdAt: Int64(Date().timeIntervalSince1970 - 100)
)
XCTAssertEqual(reply_desc(profiles: profiles, event: replyingToTwoAndMultipleOthers, locale: enUsLocale), "Replying to \(Profile.displayName(profile: nil, pubkey: "789")), \(Profile.displayName(profile: nil, pubkey: "456")) & \(othersCount) others")
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
XCTAssertNoThrow(reply_desc(profiles: profiles, event: replyingToTwoAndMultipleOthers, locale: $0))
}
}
}
}

View File

@@ -11,7 +11,7 @@ import XCTest
final class TimeAgoTests: XCTestCase {
func testTimeAgoSince() {
let locale = Locale(identifier: "en_US")
let locale = Locale(identifier: "en-US")
let calendar = locale.calendar
XCTAssertEqual(time_ago_since(Date.now, calendar), "now")