Exemple #1
0
        private static void OperationTranslateScs(string filename)
        {
            Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            Scs    scs    = Scs.Create(stream);

            stream.Close();

            WebClient client = new WebClient();

            client.BaseAddress = "http://translate.google.com";

            // mapXR.cpk trimming:
            int asbelindex = 0;
            int anmaindex  = scs.Entries.Count;

            for (int i = 0; i < scs.Entries.Count; i++)
            {
                if (scs.Entries[i] == "anma")
                {
                    anmaindex = i;
                }
                else if (scs.Entries[i] == "アスベル")
                {
                    asbelindex = i + 1;
                }
            }

            for (int i = asbelindex; i < anmaindex; i++)
            {
                string entry = scs.Entries[i];
                bool   ascii = true;
                for (int k = 0; k < entry.Length; k++)
                {
                    if ((int)entry[k] > 0xFF)
                    {
                        ascii = false;
                        break;
                    }
                }
                if (ascii)
                {
                    continue;
                }

                try {
                    MatchCollection            varmatches = Regex.Matches(entry, "[\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000A\u000B\u000C\u000D\u000E\u000F\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F]+\\(.+?\\)");
                    List <Pair <int, string> > variables  = new List <Pair <int, string> >();
                    foreach (Match match in varmatches)
                    {
                        variables.Add(new Pair <int, string>(match.Index, match.Value));
                        entry = entry.Replace(match.Value, "");
                    }

                    client.Encoding = Encoding.UTF8;
                    client.Headers["User-Agent"] = "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.3) Gecko/20100402 Namoroka/3.6.3 (.NET CLR 3.5.30729)";
                    client.QueryString.Clear();
                    client.QueryString["client"] = "t";
                    client.QueryString["sl"]     = "ja";
                    client.QueryString["tl"]     = "en";
                    client.QueryString["text"]   = entry;
                    string          ret     = client.DownloadString("/translate_a/t");
                    MatchCollection matches = Regex.Matches(ret, "\"trans\":\"(?'text'.*?)\"");
                    ret = "";
                    foreach (Match match in matches)
                    {
                        ret += match.Groups["text"].Value + "\n";
                    }
                    while (true)
                    {
                        int indexof = ret.IndexOf("\\u");
                        if (indexof < 0)
                        {
                            break;
                        }
                        int chr = int.Parse(ret.Substring(indexof + 2, 4), NumberStyles.HexNumber);
                        ret = ret.Substring(0, indexof) + (char)chr + ret.Substring(indexof + 6);
                    }
                    while (true)
                    {
                        int indexof = ret.IndexOf("\\n");
                        if (indexof < 0)
                        {
                            break;
                        }
                        ret = ret.Substring(0, indexof) + '\n' + ret.Substring(indexof + 2);
                    }
                    while (true)
                    {
                        int indexof = ret.IndexOf("\n\n");
                        if (indexof < 0)
                        {
                            break;
                        }
                        ret = ret.Substring(0, indexof) + ret.Substring(indexof + 1);
                    }
                    if (entry[entry.Length - 1] != '\n')
                    {
                        ret = ret.TrimEnd('\n');
                    }
                    if (matches.Count > 0)
                    {
                        foreach (Pair <int, string> var in variables)
                        {
                            int index = var.Key * 2;
                            if (index >= ret.Length)
                            {
                                ret = ret + var.Value;
                            }
                            else
                            {
                                for (; index > 0 && !char.IsWhiteSpace(ret[index]); index--)                                 // Find previous break
                                {
                                    ;
                                }
                                ret = ret.Substring(0, index) + var.Value + ret.Substring(index);
                            }
                        }

                        scs.Entries[i] = ret;
                    }
                    else
                    {
                        throw new Exception("Something bad happened.");
                    }
                } catch (WebException) { }
            }

            Stream ostream = new FileStream(filename + ".new", FileMode.Create, FileAccess.Write);

            scs.Save(ostream);
            ostream.Close();
        }