Example #1
0
        public string Translate(TranslatorCriteria criteria)
        {
            ApplyGlossary(criteria);

            if (criteria.By == 0)
            {
                return(criteria.Text);
            }

            var translator = TranslatorFactory.GetTranslator(criteria.By);

            return(translator.Translate(criteria.Text, criteria.From, criteria.To));
        }
Example #2
0
        public JsonResult TranslateText(string type, string text, string from, string to)
        {
            var translatorType = (Translators)Enum.Parse(typeof(Translators), type, true);
            var t  = TranslatorFactory.GetTranslator(translatorType);
            var sb = new StringBuilder();

            if (text.Contains('.'))
            {
                // Engines cannot translate more than one sentence (auto cut after period sign).
                var sentences = text.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var tt in sentences.Select(txt => t.Translate(text, @from, to)))
                {
                    sb.AppendFormat("{0}{1}", tt, tt.EndsWith(".") ? "" : "."); // some engines remove the ".", some not, so we attach it if necessary
                }
            }
            else
            {
                // single word or phrase
                sb.Append(t.Translate(text, from, to));
            }
            return(Json(sb.ToString()));
        }
Example #3
0
 private StringContent SerializeToStringContent <T>(T content, ContentType type) where T : new()
 {
     return(TranslatorFactory.GetTranslator <T>(type)
            .SerializeFrom(content)
            .ToStringContent(type));
 }
Example #4
0
        static void Main(string[] args)
        {
            var translator = TranslatorFactory.GetTranslator("cred.json");

            Console.WriteLine(translator.Translate("חיים סלמה"));
        }
Example #5
0
        protected void ProcessLines(List <List <string> > listLines,
                                    List <List <string> > listDefineLines,
                                    string sReferencePath)
        {
#warning this method is horribly long

            const string ALLOWED_LAYOUT = "allowed_layout";
            const string OVERRIDE       = "override:";

            var nDefaultCount              = CardLayout.defaultCount;
            var listColumnNames            = new List <string>();
            var dictionaryColumnNames      = new Dictionary <string, int>();
            var dictionaryElementOverrides = new Dictionary <string, Dictionary <string, int> >();
            var dictionaryDefines          = new Dictionary <string, string>();

            // Line Processing
            if (0 < listLines.Count)
            {
                // Read the column names
                var listAllColumnNames = listLines[0];
                for (int nIdx = 0; nIdx < listAllColumnNames.Count; nIdx++)
                {
                    string sKey = listAllColumnNames[nIdx].ToLower().Trim();
                    listColumnNames.Add(sKey);
                    if (!dictionaryColumnNames.ContainsKey(sKey))
                    {
                        dictionaryColumnNames.Add(sKey, nIdx);
                    }
                    else
                    {
                        IssueManager.Instance.FireAddIssueEvent("Duplicate column found in: " + sReferencePath + "::" + "Column [" + nIdx + "]: " + sKey);
                        Logger.AddLogLine("Duplicate column found in: " + sReferencePath + "::" + "Column [" + nIdx + "]: " + sKey);
                    }
                }

                // determine the allowed layout column index
                int nAllowedLayoutColumn;
                if (!dictionaryColumnNames.TryGetValue(ALLOWED_LAYOUT, out nAllowedLayoutColumn))
                {
                    nAllowedLayoutColumn = -1;
                }

                // construct the override dictionary
                foreach (string sKey in listColumnNames)
                {
                    if (sKey.StartsWith(OVERRIDE))
                    {
                        string[] arraySplit = sKey.Split(new char[] { ':' });
                        if (3 == arraySplit.Length)
                        {
                            string sElementName         = arraySplit[1].Trim();
                            string sElementItemOverride = arraySplit[2].Trim();
                            if (!dictionaryElementOverrides.ContainsKey(sElementName))
                            {
                                dictionaryElementOverrides.Add(sElementName, new Dictionary <string, int>());
                            }
                            Dictionary <string, int> dictionaryOverrides = dictionaryElementOverrides[sElementName];
                            if (dictionaryOverrides.ContainsKey(sElementItemOverride))
                            {
                                Logger.AddLogLine("Duplicate override found: {0}".FormatString(sElementItemOverride));
                            }
                            dictionaryOverrides[sElementItemOverride] = dictionaryColumnNames[sKey];
                        }
                    }
                }

                // remove the columns
                listLines.RemoveAt(0);

                // remove any lines that do not contain any values
                int nRow = 0;
                while (nRow < listLines.Count)
                {
                    var listRow   = listLines[nRow];
                    var bEmptyRow = true;
                    foreach (string sCol in listRow)
                    {
                        if (0 < sCol.Trim().Length)
                        {
                            bEmptyRow = false;
                            break;
                        }
                    }
                    if (bEmptyRow)
                    {
                        listLines.RemoveAt(nRow);
                    }
                    else
                    {
                        nRow++;
                    }
                }

                // remove any layout elements that do not match the card layout
                if (-1 != nAllowedLayoutColumn)
                {
                    int nLine = 0;

                    while (nLine < listLines.Count)
                    {
                        if (!CardLayout.Name.Equals(listLines[nLine][nAllowedLayoutColumn], StringComparison.CurrentCultureIgnoreCase))
                        {
                            listLines.RemoveAt(nLine);
                        }
                        else
                        {
                            nLine++;
                        }
                    }
                }

                // indicate if no lines remain!
                if (0 == listLines.Count)
                {
                    IssueManager.Instance.FireAddIssueEvent("No lines found?! allowed_layout may be cutting them!");
                }
            }

            ValidLines.Clear();

            // create the duplicated lines (with count > 0)
            foreach (List <string> listItems in listLines)
            {
                if (0 < listItems.Count)
                {
                    int nNumber;
                    if (!int.TryParse(listItems[0].Trim(), out nNumber))
                    {
                        IssueManager.Instance.FireAddIssueEvent("Invalid card count found: [" + listItems[0] + "] The first column should always have a number value.");
                        nNumber = 1;
                    }
                    for (var nCount = 0; nCount < nNumber; nCount++)
                    {
                        ValidLines.Add(new DeckLine(listItems, nCount));
                    }
                }
            }
            // always create a line
            if (0 == ValidLines.Count && 0 < nDefaultCount)
            {
                // create the default number of lines.
                for (int nIdx = 0; nIdx < nDefaultCount; nIdx++)
                {
                    if (0 < listColumnNames.Count)// create each line and the correct number of columns
                    {
                        var arrayDefaultLine = new List <string>();
                        for (int nCol = 0; nCol < arrayDefaultLine.Count; nCol++)
                        {
                            arrayDefaultLine.Add(string.Empty);
                        }
                        ValidLines.Add(new DeckLine(arrayDefaultLine));
                    }
                    else // no columns just create an empty row
                    {
                        ValidLines.Add(new DeckLine(new List <string>()));
                    }
                }
                if (!string.IsNullOrEmpty(sReferencePath))
                {
                    IssueManager.Instance.FireAddIssueEvent("No lines found for this layout! Generated " + nDefaultCount);
                }
            }

            // Define Processing
            // remove the column names
            if (listDefineLines.Count > 0)
            {
                // the removal of the first row happens in the reader(s) for defines
//                listDefineLines.RemoveAt(0);
                foreach (var row in listDefineLines)
                {
                    if (row.Count > 1)
                    {
                        string sKey = row[0];
                        int    nIdx;
                        string sVal;
                        if (dictionaryDefines.TryGetValue(sKey.ToLower(), out sVal))
                        {
                            string sMsg = "Duplicate define found: " + sKey;
                            IssueManager.Instance.FireAddIssueEvent(sMsg);
                            Logger.AddLogLine(sMsg);
                        }
                        else if (dictionaryColumnNames.TryGetValue(sKey.ToLower(), out nIdx))
                        {
                            string sMsg = "Overlapping column name and define found in: " + sReferencePath + "::" + "Column [" + nIdx + "]: " + sKey;
                            IssueManager.Instance.FireAddIssueEvent(sMsg);
                            Logger.AddLogLine(sMsg);
                        }
                        else
                        {
                            dictionaryDefines.Add(sKey.ToLower(), row[1]);
                        }
                    }
                }
            }

            m_zTranslator = TranslatorFactory.GetTranslator(dictionaryColumnNames, dictionaryDefines, dictionaryElementOverrides, listColumnNames);

#if MONO_BUILD
            Thread.Sleep(100);
#endif
            WaitDialog.Instance.ThreadSuccess = true;
            WaitDialog.Instance.CloseWaitDialog();
        }
Example #6
0
        public T ResultAs <T>(ContentType type = ContentType.JSON) where T : new()
        {
            IDeserializer <T> serializer = TranslatorFactory.GetTranslator <T>(type);

            return(serializer.DeserializeFrom(Content));
        }