예제 #1
0
        public TrStringNumInfo(TrStringNum orig, TrStringNum trans, NumberSystem origNumberSystem, NumberSystem transNumberSystem)
        {
            NewOriginal       = orig.Translations;
            TranslationTr     = trans;
            OriginalNumSys    = origNumberSystem;
            TranslationNumSys = transNumberSystem;

            int numberOfNumbers = trans.IsNumber.Where(b => b).Count();
        }
예제 #2
0
파일: TrString.cs 프로젝트: biorpg/RT.Util
        /// <summary>
        ///     Selects the correct string and interpolates the specified arguments.</summary>
        /// <param name="ns">
        ///     Number system to use to interpolate the translation.</param>
        /// <param name="args">
        ///     Arguments to be interpolated into the translation.</param>
        public string Fmt(NumberSystem ns, params object[] args)
        {
            try
            {
                int n = 0;
                int m = 1;
                for (int i = 0; i < IsNumber.Length; i++)
                {
                    if (IsNumber[i])
                    {
                        double numD = 0;
                        int    numI;
                        bool   isInteger;
                        if (args[i] is double || args[i] is float || args[i] is decimal)
                        {
                            numD      = ExactConvert.ToDouble(args[i]);
                            numI      = unchecked ((int)numD);
                            isInteger = numD == (double)numI;
                        }
                        else if (args[i] is int || args[i] is uint || args[i] is long || args[i] is ulong || args[i] is short || args[i] is ushort || args[i] is byte || args[i] is sbyte)
                        {
                            numI      = ExactConvert.ToInt(args[i]);
                            isInteger = true;
                        }
                        else
                        {
                            throw new ArgumentException("Argument #{0} was expected to be a number, but a {1} was given.".Fmt(i, args[i].GetType().FullName), "args");
                        }

                        if (isInteger)
                        {
                            n += ns.GetString(numI) * m;
                        }
                        else
                        {
                            n += ns.GetString(numD) * m;
                        }

                        m *= ns.NumStrings;
                    }
                }
                return(Translations[n].Fmt(args));
            }
            catch
            {
                if (Translations != null && Translations.Length > 0)
                {
                    return(Translations[0]);
                }
                else
                {
                    return("(NO STRING)");
                }
            }
        }
예제 #3
0
 /// <summary>
 ///     Constructor.</summary>
 /// <param name="languageCode">
 ///     Specifies the ISO-639 language code of the language.</param>
 /// <param name="englishName">
 ///     Specifies the English name of the language.</param>
 /// <param name="nativeName">
 ///     Specifies the native name of the language.</param>
 /// <param name="numberSystem">
 ///     Specifies the number system of the language.</param>
 public LanguageInfoAttribute(string languageCode, string englishName, string nativeName, Type numberSystem)
 {
     _languageCode = languageCode;
     _englishName  = englishName;
     _nativeName   = nativeName;
     if (numberSystem == typeof(NumberSystem))
     {
         _numberSystem = null;
     }
     else if (typeof(NumberSystem).IsAssignableFrom(numberSystem))
     {
         _numberSystem = (NumberSystem)numberSystem.GetConstructor(Type.EmptyTypes).Invoke(null);
     }
     else
     {
         _numberSystem = null;
     }
 }
예제 #4
0
        private static void getGroups(FieldInfo field, Type type, object original, object translation,
                                      NumberSystem originalNumSys, NumberSystem translationNumSys, Dictionary <object, TranslationGroup> dic,
                                      ref TranslationGroup ungrouped, IEnumerable <object> classGroups, string path)
        {
            if (!type.IsDefined <LingoStringClassAttribute>(true))
            {
                if (field == null)
                {
                    throw new ArgumentException($@"Type ""{type.FullName}"" must be marked with the [LingoStringClass] attribute.", nameof(type));
                }
                else
                {
                    throw new ArgumentException($@"Field ""{field.DeclaringType.FullName}.{field.Name}"" must either be marked with the [LingoIgnore] attribute, or be of type TrString, TrStringNumbers, or a type with the [LingoStringClass] attribute.", nameof(type));
                }
            }

            var thisClassGroups = type.GetCustomAttributes(true).OfType <LingoInGroupAttribute>().Select(attr => attr.Group);

            if (classGroups != null)
            {
                thisClassGroups = thisClassGroups.Concat(classGroups);
            }

            foreach (var f in type.GetFields(BindingFlags.Public | BindingFlags.Instance))
            {
                if (f.FieldType == typeof(TrString) || f.FieldType == typeof(TrStringNum))
                {
                    string notes  = f.GetCustomAttributes <LingoNotesAttribute>().Select(lna => lna.Notes).FirstOrDefault();
                    var    trInfo = f.FieldType == typeof(TrString)
                        ? (TranslationInfo) new TrStringInfo
                    {
                        Label         = path + f.Name,
                        Notes         = notes,
                        NewOriginal   = ((TrString)f.GetValue(original)).Translation,
                        TranslationTr = (TrString)f.GetValue(translation)
                    }
                        : (TranslationInfo) new TrStringNumInfo((TrStringNum)f.GetValue(original), (TrStringNum)f.GetValue(translation), originalNumSys, translationNumSys)
                    {
                        Label = path + f.Name,
                        Notes = notes
                    };

                    var groups = f.GetCustomAttributes <LingoInGroupAttribute>().Select(attr => attr.Group).Concat(thisClassGroups);
                    if (!groups.Any())
                    {
                        if (ungrouped == null)
                        {
                            ungrouped = new TranslationGroup {
                                Label = "Ungrouped strings", Notes = "This group contains strings not found in any other group."
                            }
                        }
                        ;
                        ungrouped.Infos.Add(trInfo);
                    }
                    else
                    {
                        foreach (var group in groups)
                        {
                            TranslationGroup grp;
                            if (!dic.TryGetValue(group, out grp))
                            {
                                grp        = createGroup(group);
                                dic[group] = grp;
                            }
                            grp.Infos.Add(trInfo);
                        }
                    }
                }
                else if (!f.IsDefined <LingoIgnoreAttribute>(true))
                {
                    getGroups(f, f.FieldType, f.GetValue(original), f.GetValue(translation), originalNumSys, translationNumSys, dic, ref ungrouped, thisClassGroups, path + f.Name + " / ");
                }
            }
        }
예제 #5
0
        private void populateRows(Grid grid, string label, TrStringNumInfo info, string[] display, NumberSystem ns, bool useTextBoxes, int nn, ref int curRow, string[] newOriginal = null)
        {
            int numRows = (int)Math.Pow(ns.NumStrings, nn);

            var textBlock = new TextBlock(new Run(label));

            grid.Children.Add(textBlock);
            Grid.SetColumn(textBlock, 0);
            Grid.SetRow(textBlock, curRow);
            Grid.SetRowSpan(textBlock, numRows);

            var textBoxes = useTextBoxes ? new List <TextBox>() : null;

            int column = 1;

            for (int i = 0; i < info.TranslationTr.IsNumber.Length; i++)
            {
                if (info.TranslationTr.IsNumber[i])
                {
                    var header = new TextBlock(new Run("{" + i + "}"));
                    Grid.SetColumn(header, column);
                    Grid.SetRow(header, curRow);
                    grid.Children.Add(header);
                    column++;
                }
            }
            curRow++;

            Button button = null;

            for (int row = 0; row < numRows; row++)
            {
                int col = 1;
                int r   = row;
                for (int i = 0; i < info.TranslationTr.IsNumber.Length; i++)
                {
                    if (info.TranslationTr.IsNumber[i])
                    {
                        var numFormDescr = new TextBlock(new Run(ns.GetDescription(r % ns.NumStrings)));
                        Grid.SetColumn(numFormDescr, col);
                        Grid.SetRow(numFormDescr, curRow);
                        grid.Children.Add(numFormDescr);
                        col++;
                        r /= ns.NumStrings;
                    }
                }
                if (useTextBoxes)
                {
                    var textBox = new TextBox
                    {
                        Text = (display != null && row < display.Length) ? display[row] : "",
                        VerticalAlignment   = VerticalAlignment.Center,
                        HorizontalAlignment = HorizontalAlignment.Stretch,
                        Width         = double.NaN,
                        Height        = double.NaN,
                        AcceptsReturn = true,
                        Tag           = info
                    };
                    Grid.SetColumn(textBox, col);
                    Grid.SetRow(textBox, curRow);
                    grid.Children.Add(textBox);
                    if (row == 0)
                    {
                        button = new Button {
                            Content = "OK", Tag = info
                        };
                        Grid.SetColumn(button, col + 1);
                        Grid.SetRow(button, curRow);
                        Grid.SetRowSpan(button, numRows);
                        button.Click += delegate
                        {
                            var newTrans = textBoxes.Select(box => box.Text).ToArray();
                            if (info.State != TranslationInfoState.UpToDateAndSaved || !info.TranslationTr.Translations.SequenceEqual(newTrans))
                            {
                                info.TranslationTr.Translations = newTrans;
                                info.TranslationTr.Old          = newOriginal;
                                _anyChanges = true;
                            }
                            fireCtrlDown();
                        };
                        // Do not add to the grid yet; must add it after all the textboxes so that its tab order is correct
                    }
                }
                else
                {
                    var originalText = new TextBlock(new Run((display != null && row < display.Length) ? display[row] : ""));
                    Grid.SetColumn(originalText, col);
                    Grid.SetColumnSpan(originalText, 2);
                    Grid.SetRow(originalText, curRow);
                    grid.Children.Add(originalText);
                }
                curRow++;
            }

            // Defer adding the button until the end so that its tab order is correct
            if (button != null)
            {
                grid.Children.Add(button);
            }
        }