@@ -19,19 +19,29 @@ struct TolgeeTestsEn {
1919 }
2020 },
2121 " My name is %@ " : " My name is %@ " ,
22- " Redraw " : " Redraw "
22+ " Redraw " : " Redraw " ,
23+ " name_and_num_apples " : " My name is %@ and I have %lld apples "
24+ }
25+ """
26+
27+ let testTranslationsJSONCustomNamespace = """
28+ {
29+ " hello_world " : " Hello, world from custom namespace! "
2330 }
2431 """
2532
2633 let englishLocale = Locale ( identifier: " en_US " )
2734
28- @Test func testLoadEnglishTranslationsFromJSON( ) throws {
35+ @Test func testLoadEnglishTranslationsFromJSON( ) async throws {
2936
3037 let context = TestContext ( )
3138 let tolgee = context. tolgee
3239 tolgee. initialize ( cdn: URL ( string: " https://cdn.example.com " ) !, language: " en " )
3340
34- try tolgee. loadTranslations ( from: testTranslationsJSON)
41+ await context. urlSession. setMockResponse (
42+ for: URL ( string: " https://cdn.example.com/en.json " ) !,
43+ result: . success( Data ( testTranslationsJSON. utf8) ) )
44+ await tolgee. remoteFetch ( )
3545
3646 if #available( macOS 15 . 4 , * ) {
3747
@@ -44,11 +54,15 @@ struct TolgeeTestsEn {
4454 }
4555 }
4656
47- @Test func testEnglishSimplePlaceholderReplacement( ) throws {
57+ @Test func testEnglishSimplePlaceholderReplacement( ) async throws {
4858 let context = TestContext ( )
4959 let tolgee = context. tolgee
5060 tolgee. initialize ( cdn: URL ( string: " https://cdn.example.com " ) !, language: " en " )
51- try tolgee. loadTranslations ( from: testTranslationsJSON)
61+
62+ await context. urlSession. setMockResponse (
63+ for: URL ( string: " https://cdn.example.com/en.json " ) !,
64+ result: . success( Data ( testTranslationsJSON. utf8) ) )
65+ await tolgee. remoteFetch ( )
5266
5367 if #available( macOS 15 . 4 , * ) {
5468
@@ -64,11 +78,15 @@ struct TolgeeTestsEn {
6478 }
6579 }
6680
67- @Test func testEnglishPluralFormsWithHashReplacement( ) throws {
81+ @Test func testEnglishPluralFormsWithHashReplacement( ) async throws {
6882 let context = TestContext ( )
6983 let tolgee = context. tolgee
7084 tolgee. initialize ( cdn: URL ( string: " https://cdn.example.com " ) !, language: " en " )
71- try tolgee. loadTranslations ( from: testTranslationsJSON)
85+
86+ await context. urlSession. setMockResponse (
87+ for: URL ( string: " https://cdn.example.com/en.json " ) !,
88+ result: . success( Data ( testTranslationsJSON. utf8) ) )
89+ await tolgee. remoteFetch ( )
7290
7391 if #available( macOS 15 . 4 , * ) {
7492
@@ -93,14 +111,78 @@ struct TolgeeTestsEn {
93111 }
94112 }
95113
96- @Test func testEnglishMissingTranslationFallback( ) throws {
114+ @Test func testEnglishMissingTranslationFallback( ) async throws {
97115 let context = TestContext ( )
98116 let tolgee = context. tolgee
99117 tolgee. initialize ( cdn: URL ( string: " https://cdn.example.com " ) !, language: " en " )
100- try tolgee. loadTranslations ( from: testTranslationsJSON)
118+
119+ await context. urlSession. setMockResponse (
120+ for: URL ( string: " https://cdn.example.com/en.json " ) !,
121+ result: . success( Data ( testTranslationsJSON. utf8) ) )
122+ await tolgee. remoteFetch ( )
101123
102124 // Test with a key that doesn't exist (should fallback to NSLocalizedString)
103125 let missingKey = tolgee. translate ( " nonexistent.key " )
104126 #expect( missingKey == " nonexistent.key " )
105127 }
128+
129+ @Test func testEnglishNameAndNumberPlaceholderReplacement( ) async throws {
130+ let context = TestContext ( )
131+ let tolgee = context. tolgee
132+ tolgee. initialize ( cdn: URL ( string: " https://cdn.example.com " ) !, language: " en " )
133+
134+ await context. urlSession. setMockResponse (
135+ for: URL ( string: " https://cdn.example.com/en.json " ) !,
136+ result: . success( Data ( testTranslationsJSON. utf8) ) )
137+ await tolgee. remoteFetch ( )
138+
139+ if #available( macOS 15 . 4 , * ) {
140+
141+ // Test English name and number replacement
142+ let nameAndNumber = tolgee. translate ( " name_and_num_apples " , " John " , 5 , locale: englishLocale)
143+ #expect( nameAndNumber == " My name is John and I have 5 apples " )
144+
145+ let anotherNameAndNumber = tolgee. translate (
146+ " name_and_num_apples " , " Alice " , 1 , locale: englishLocale)
147+ #expect( anotherNameAndNumber == " My name is Alice and I have 1 apples " )
148+
149+ let zeroApples = tolgee. translate ( " name_and_num_apples " , " Bob " , 0 , locale: englishLocale)
150+ #expect( zeroApples == " My name is Bob and I have 0 apples " )
151+
152+ } else {
153+ #expect( Bool ( false ) ) // Skip this test on older versions
154+ }
155+ }
156+
157+ @Test func testLoadEnglishTranslationsFromCustomNamespace( ) async throws {
158+ let context = TestContext ( )
159+ let tolgee = context. tolgee
160+ tolgee. initialize (
161+ cdn: URL ( string: " https://cdn.example.com " ) !, language: " en " , namespaces: [ " namespace " ] ,
162+ enableDebugLogs: true )
163+
164+ await context. urlSession. setMockResponse (
165+ for: URL ( string: " https://cdn.example.com/en.json " ) !,
166+ result: . success( Data ( testTranslationsJSON. utf8) ) )
167+
168+ await context. urlSession. setMockResponse (
169+ for: URL ( string: " https://cdn.example.com/namespace/en.json " ) !,
170+ result: . success( Data ( testTranslationsJSONCustomNamespace. utf8) ) )
171+
172+ await tolgee. remoteFetch ( )
173+
174+ if #available( macOS 15 . 4 , * ) {
175+
176+ // Test basic English translation from custom namespace
177+ let greeting = tolgee. translate ( " hello_world " , table: " namespace " , locale: englishLocale)
178+ #expect( greeting == " Hello, world from custom namespace! " )
179+
180+ // Test that keys from the main namespace are not available
181+ let missingKey = tolgee. translate ( " Hello, world! " , table: " namespace " , locale: englishLocale)
182+ #expect( missingKey == " Hello, world! " ) // Should fallback to the key itself
183+
184+ } else {
185+ #expect( Bool ( false ) ) // Skip this test on older versions
186+ }
187+ }
106188}
0 commit comments