diff --git a/Sources/TranslationCatalogIO/Internal/StringsXml.swift b/Sources/TranslationCatalogIO/Internal/StringsXml.swift index 5ca2c61..c268eea 100644 --- a/Sources/TranslationCatalogIO/Internal/StringsXml.swift +++ b/Sources/TranslationCatalogIO/Internal/StringsXml.swift @@ -29,7 +29,7 @@ struct StringsXml: Codable, DynamicNodeDecoding, DynamicNodeEncoding { func encoded() throws -> Data { let encoder = XMLEncoder() encoder.outputFormatting = [.sortedKeys, .prettyPrinted] - return try encoder.encode( + let encoded = try encoder.encode( self, withRootKey: "resources", header: XMLHeader( @@ -37,6 +37,10 @@ struct StringsXml: Codable, DynamicNodeDecoding, DynamicNodeEncoding { encoding: "UTF-8" ) ) + var string = String(decoding: encoded, as: UTF8.self) + string = string.replacingOccurrences(of: "'", with: "\\'") + string = string.replacingOccurrences(of: " ", with: " ") // Non-Breaking Space + return string.data(using: .utf8) ?? encoded } } diff --git a/Tests/TranslationCatalogTests/ExpressionEncoderTests.swift b/Tests/TranslationCatalogTests/ExpressionEncoderTests.swift index bf8f0f9..2b0a9e2 100644 --- a/Tests/TranslationCatalogTests/ExpressionEncoderTests.swift +++ b/Tests/TranslationCatalogTests/ExpressionEncoderTests.swift @@ -26,7 +26,7 @@ struct ExpressionEncoderTests { Expression( id: UUID(uuidString: "5870E853-35B9-4C71-B8D6-7EA922C72835")!, key: "EXP_03", - value: "Hello %s, welcome to %s!", + value: "Hello %s, welcome to '%s'!", // 'Hello{Non-Breaking-Space}%s languageCode: .english ), ] @@ -44,7 +44,7 @@ struct ExpressionEncoderTests { Hello World! Hello %s! - Hello %s, welcome to %s! + Hello %s, welcome to \\'%s\\'! """) } @@ -60,7 +60,7 @@ struct ExpressionEncoderTests { #expect(output == """ "EXP_01" = "Hello World!"; "EXP_02" = "Hello %@!"; - "EXP_03" = "Hello %@, welcome to %@!"; + "EXP_03" = "Hello\\U00A0%@, welcome to '%@'!"; """) } @@ -76,7 +76,7 @@ struct ExpressionEncoderTests { { "EXP_01" : "Hello World!", "EXP_02" : "Hello %s!", - "EXP_03" : "Hello %s, welcome to %s!" + "EXP_03" : "Hello %s, welcome to '%s'!" } """) }