Example #1
0
 private void AddStringToTranslationCache(string sKey, ElementString zElementString)
 {
     if (m_dictionaryElementStringCache.ContainsKey(sKey))
     {
         m_dictionaryElementStringCache.Remove(sKey);
         //Logger.AddLogLine("String Cache: Replace?!");
     }
     m_dictionaryElementStringCache.Add(sKey, zElementString);
 }
Example #2
0
        /// <summary>
        /// Translates the string representing the element. (also handles any nodraw text input)
        /// </summary>
        /// <param name="sRawString"></param>
        /// <param name="listLine"></param>
        /// <param name="zElement"></param>
        /// <param name="bPrint"></param>
        /// <returns></returns>
        public ElementString TranslateString(string sRawString, DeckLine zDeckLine, ProjectLayoutElement zElement, bool bPrint)
        {
            List<string> listLine = zDeckLine.LineColumns;

            ElementString zCached;
            if (m_dictionaryElementStringCache.TryGetValue(zElement.name, out zCached))
            {
                return zCached;
            } 
            
            string sOutput = sRawString;

            sOutput = sOutput.Replace("#empty", string.Empty);

            var zElementString = new ElementString();

            // Translate named items (column names / defines)
            //Groups
            //    1    2    3   4   5
            //@"(.*)(@\[)(.+?)(\])(.*)"
            Match zMatch;
            while (s_regexColumnVariable.IsMatch(sOutput))
            {
                zMatch = s_regexColumnVariable.Match(sOutput);
                int nIndex;
                string sDefineValue;
                var sKey = zMatch.Groups[3].ToString().ToLower();

                // check the key for untranslated components
                var arrayParams = sKey.Split(new char[] {','});
                if (arrayParams.Length > 1)
                {
                    sKey = arrayParams[0];
                }

                if (m_dictionaryDefines.TryGetValue(sKey, out sDefineValue))
                {
                }
                else if (m_dictionaryColumnNames.TryGetValue(sKey, out nIndex))
                {
                    sDefineValue = (nIndex >= listLine.Count ? string.Empty : listLine[nIndex].Trim());
                }
                else
                {
                    MDIIssues.Instance.AddIssue("Bad reference key: " + sKey);
                    sDefineValue = "[BAD NAME: " + sKey + "]";
                }
                if (arrayParams.Length > 1)
                {
                    for (int nIdx = 1; nIdx < arrayParams.Length; nIdx++)
                    {
                        sDefineValue = sDefineValue.Replace("{" + nIdx + "}", arrayParams[nIdx]);
                    }
                }
                sOutput = zMatch.Groups[1] + sDefineValue + zMatch.Groups[5];
            }
            
            // Translate card counter/index
            // Groups                 
            //     1   2    3  4    5  6    7  8   9
            //(@"(.*)(##)(\d+)(;)(\d+)(;)(\d+)(#)(.*)");
            while (s_regexCardCounter.IsMatch(sOutput))
            {
                zMatch = s_regexCardCounter.Match(sOutput);
                var nStart = Int32.Parse(zMatch.Groups[3].ToString());
                var nChange = Int32.Parse(zMatch.Groups[5].ToString());
                var nLeftPad = Int32.Parse(zMatch.Groups[7].ToString());

                var nIndex = bPrint ? m_nCardPrintIndex : m_nCardIndex;

                sOutput = zMatch.Groups[1] +
                    // nIndex is left as is (not adding 1)
                    (nStart + (nIndex * nChange)).ToString(CultureInfo.InvariantCulture).PadLeft(nLeftPad, '0') +
                    zMatch.Groups[9];
            }

            // Translate sub card counter/index
            // Groups                 
            //     1   2    3  4    5  6    7  8   9
            //(@"(.*)(#sc;)(\d+)(;)(\d+)(;)(\d+)(#)(.*)");
            while (s_regexSubCardCounter.IsMatch(sOutput))
            {
                zMatch = s_regexSubCardCounter.Match(sOutput);
                var nStart = Int32.Parse(zMatch.Groups[3].ToString());
                var nChange = Int32.Parse(zMatch.Groups[5].ToString());
                var nLeftPad = Int32.Parse(zMatch.Groups[7].ToString());

                var nIndex = zDeckLine.RowSubIndex;

                sOutput = zMatch.Groups[1] +
                    // nIndex is left as is (not adding 1)
                    (nStart + (nIndex * nChange)).ToString(CultureInfo.InvariantCulture).PadLeft(nLeftPad, '0') +
                    zMatch.Groups[9];
            }

            // Translate If Logic
            //Groups
            //    1     2    3    4   5 
            //@"(.*)(#\()(if.+)(\)#)(.*)");
            while (s_regexIfLogic.IsMatch(sOutput))
            {
                zMatch = s_regexIfLogic.Match(sOutput);
                string sLogicResult = TranslateIfLogic(zMatch.Groups[3].ToString());
                if (sLogicResult.Trim().Equals("#nodraw", StringComparison.CurrentCultureIgnoreCase))
                    zElementString.DrawElement = false;
                sOutput = zMatch.Groups[1] +
                    sLogicResult +
                    zMatch.Groups[5];
            }

            // Translate Switch Logic
            //Groups                  
            //    1     2        3    4   5 
            //@"(.*)(#\()(switch.+)(\)#)(.*)");
            while (s_regexSwitchLogic.IsMatch(sOutput))
            {
                zMatch = s_regexSwitchLogic.Match(sOutput);
                string sLogicResult = TranslateSwitchLogic(zMatch.Groups[3].ToString());
                if (sLogicResult.Trim().Equals("#nodraw", StringComparison.CurrentCultureIgnoreCase))
                    zElementString.DrawElement = false;
                
                sOutput = zMatch.Groups[1] +
                    sLogicResult +
                    zMatch.Groups[5];
            }

            switch ((ElementType)Enum.Parse(typeof(ElementType), zElement.type))
            {
                case ElementType.Text:
                    sOutput = sOutput.Replace("\\n", Environment.NewLine);
                    sOutput = sOutput.Replace("\\q", "\"");
                    sOutput = sOutput.Replace("\\c", ",");
                    sOutput = sOutput.Replace("&gt;", ">");
                    sOutput = sOutput.Replace("&lt;", "<");
                    break;
                case ElementType.FormattedText:
                    sOutput = sOutput.Replace("<c>", ",");
                    sOutput = sOutput.Replace("<q>", "\"");
                    sOutput = sOutput.Replace("&gt;", ">");
                    sOutput = sOutput.Replace("&lt;", "<");
                    break;
            }

            zElementString.String = sOutput;

            AddStringToTranslationCache(zElement.name, zElementString);

            return zElementString;
        }