/iBlue.ResxHelper/Src/Program.fs
F# | 151 lines | 134 code | 14 blank | 3 comment | 17 complexity | 992e93b291ff5364fd6993c4d0a0fecd MD5 | raw file
1// Learn more about F# at http://fsharp.net 2open System 3open System.Net 4open System.IO 5open Microsoft.FSharp.Control.WebExtensions 6#if INTERACTIVE 7#load @"AsyncHelpers.fs" 8#endif 9open FSharp.Tutorial 10open System.Collections.Generic 11open System.Linq 12 13let myAppId = "B3CFD35A9EB3995659BAC3E9B6456CE8008AD59B" 14let translateUri = "http://api.microsofttranslator.com/V1/Http.svc/Translate?appId=" + myAppId + "&" 15let languageUri = "http://api.microsofttranslator.com/V1/Http.svc/GetLanguages?appId=" + myAppId 16let languageNameUri = "http://api.microsofttranslator.com/V1/Http.svc/GetLanguageNames?appId=" + myAppId 17let run x = Async.RunSynchronously x 18 19let httpLines (uri:string) = 20 async { 21 let request = WebRequest.Create uri 22 use! response = request.AsyncGetResponse() 23 use stream = response.GetResponseStream() 24 use reader = new StreamReader(stream) 25 let! lines = reader.AsyncReadAllLines() 26 return new List<String>(lines |> List.toArray) 27 } 28 29/// key is just a variable for concatinating outputs 30let translateText(key, text, fromLang, toLang) = 31 async { 32 let uri = sprintf "%sfrom=%s&to=%s" translateUri fromLang toLang 33 let req2 = WebRequest.Create(uri, Method = "Post", ContentType = "text/plain") 34 do! req2.AsyncWriteContent text 35 let! resp = req2.AsyncReadResponse() 36 return key + resp 37 } 38 39let translateFile(filename : string, fromLang : string, toLang : string, outputfile : string) = 40 let languages = httpLines languageUri |> run 41 printfn "Languages available" 42 languages 43 |> Seq.iteri(fun i t -> printfn "%d) %s" i t) 44 45 use sr = new StreamReader(filename) 46 let lines = sr.ReadToEnd().Split([|Environment.NewLine|], StringSplitOptions.RemoveEmptyEntries) 47 printfn "Language Translation started..." 48 let task = 49 Async.Parallel 50 [for line in lines -> 51 let txt = line.Split([|'='|]) 52 let key = txt.[0] + "=" 53 translateText(key, txt.[1], fromLang, toLang)] 54 Async.StartWithContinuations( 55 task, 56 (fun results -> 57 let sb = new System.Text.StringBuilder() 58 for r in results do 59 printfn "%s" r 60 sb.AppendLine(r) |> ignore 61 let ofile = 62 if outputfile = String.Empty then 63 let f = filename.Substring(0, filename.IndexOf(".")) 64 f + "." + toLang + ".txt" 65 else 66 outputfile 67 use sw = new StreamWriter(ofile) 68 sw.Write(sb.ToString()) 69 printfn "Language Translation done..." 70 ), 71 (fun _ -> ()), 72 (fun _ -> ())) 73 74#if INTERACTIVE 75printf "From Lang:" 76let fLang = Console.ReadLine() 77printf "To Lang:" 78let tLang = Console.ReadLine() 79printf "Enter Resouces File Path:" 80let filename = Console.ReadLine() 81 82if fLang <> String.Empty && languages.Contains(fLang) && tLang <> String.Empty && languages.Contains(tLang) && filename <> String.Empty then 83 translateFile(filename, fLang, tLang, String.Empty) 84else 85 printfn "Input details in-correct" 86#else 87/// /file:sometext.txt /from:en /to:de /o:output.de 88[<EntryPoint>] 89let main args = 90 let usage = "Usage of ResxGenerator should be as below, 91 92 resxgenerator.exe /f:sometext.txt /from:en /to:de /o:output.de.txt 93 94 /f: - filename 95 /from: - from language 96 /to: - to language 97 /o: - output file" 98 if args.Length = 0 then 99 printfn "%s" usage 100 else 101 try 102 let parseArg(cmp) = 103 let bitString = args.FirstOrDefault(Func<string, bool>(fun s -> s.Contains(cmp))) 104 if bitString <> String.Empty then 105 let b = bitString.Split([|':'|]) 106 Some(b.[0], b.[1]) 107 else 108 None 109 let parseFileArg(cmp) = 110 let bitString = args.FirstOrDefault(Func<string, bool>(fun s -> s.Contains(cmp))) 111 if bitString <> String.Empty then 112 let s1 = bitString 113 Some(s1.Substring(s1.IndexOf("/f:") + 3, s1.Length - (s1.IndexOf("/f:") + 3))) 114 else 115 None 116 117 let fileArg = parseFileArg("/f:") 118 let filename = 119 match fileArg with 120 | Some(b) -> b 121 | _ -> String.Empty 122 123 let fromArg = parseArg("/from:") 124 let fLang = 125 match fromArg with 126 | Some(b, b1) -> b1 127 | _ -> String.Empty 128 129 let toArg = parseArg("/to:") 130 let tLang = 131 match toArg with 132 | Some(b, b1) -> b1 133 | _ -> String.Empty 134 135 let outputfileArg = parseArg("/o:") 136 let outputfile = 137 match outputfileArg with 138 | Some(b, b1) -> b1 139 | _ -> "output.txt" 140 141 if fLang <> String.Empty && tLang <> String.Empty && filename <> String.Empty then 142 translateFile(filename, fLang, tLang, outputfile) 143 else 144 printfn "%s" usage 145 Console.ReadKey() |> ignore 146 with 147 | e -> 148 printfn "%s" e.Message 149 printfn "%s" usage 150 0 151#endif