-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFormat.Android.fs
48 lines (43 loc) · 1.21 KB
/
Format.Android.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Android
open Model
open System.IO
open System.Text
open System.Xml
let private getPath name lang =
Option.defaultValue "strings" name
|> match lang with
| Column.Default -> sprintf "values/%s.xml"
| s -> sprintf "values-%s/%s.xml" s
let private stringEncode (str:string) =
str
.Replace("\\", "\\\\")
.Replace("\n", "\\n")
.Replace("@", "\\@")
.Replace("?","\\?")
.Replace("'","\\'")
.Replace("\"","\\\"")
let private formatPhrases ph phrases =
let settings = XmlWriterSettings()
settings.Indent <- true
settings.Encoding <- UTF8Encoding false
use output = new MemoryStream()
use xml = XmlTextWriter.Create (output,settings)
xml.WriteStartDocument ()
xml.WriteStartElement "resources"
for phrase in phrases do
let value =
phrase.Value
|> ph (fun _ -> "%s")
|> stringEncode
xml.WriteStartElement "string"
xml.WriteAttributeString ("name",phrase.Key)
xml.WriteValue (value)
xml.WriteEndElement ()
xml.WriteEndDocument ()
xml.Close ()
Encoding.Default.GetString (output.ToArray())
let format name ph lang phrases = {
Path = (getPath name lang)
Contents = (formatPhrases ph phrases)
Phrases = Seq.length phrases
}