Example #1
0
        /// <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)");
                }
            }
        }
Example #2
0
            protected override void writeToStreamImpl(Stream stream)
            {
                if (DataType == DataType.DictionaryOther)
                {
                    stream.WriteByte((byte)KeyType);
                }

                foreach (var kvp in Dictionary)
                {
                    // Store value first
                    kvp.Value.WriteToStream(stream);

                    // Then store key
                    switch (DataType)
                    {
                    case DataType.DictionaryInt64:
                        stream.WriteInt32Optim(ExactConvert.ToInt(kvp.Key));
                        break;

                    case DataType.DictionaryString:
                        WriteBuffer(stream, ExactConvert.ToString(kvp.Key).ToUtf8(), true);
                        break;

                    case DataType.DictionaryTwoStrings:
                        if (kvp.Key is string)
                        {
                            WriteBuffer(stream, ((string)kvp.Key).ToUtf8(), true);
                            WriteBuffer(stream, new byte[0], true);
                        }
                        else
                        {
                            var fn = (FieldNameWithType)kvp.Key;
                            WriteBuffer(stream, fn.FieldName.ToUtf8(), true);
                            WriteBuffer(stream, fn.DeclaringType == null ? new byte[0] : fn.DeclaringType.ToUtf8(), true);
                        }
                        break;

                    case DataType.DictionaryOther:
                        switch (KeyType)
                        {
                        case DataType.UInt64:
                            stream.WriteUInt64Optim(ExactConvert.ToULong(kvp.Key));
                            break;

                        case DataType.Single:
                            stream.Write(BitConverter.GetBytes(ExactConvert.ToFloat(kvp.Key)));
                            break;

                        case DataType.Double:
                            stream.Write(BitConverter.GetBytes(ExactConvert.ToDouble(kvp.Key)));
                            break;

                        case DataType.DateTime:
                            stream.Write(BitConverter.GetBytes(((DateTime)kvp.Key).ToBinary()));
                            break;

                        case DataType.Decimal:
                            stream.WriteDecimalOptim(ExactConvert.ToDecimal(kvp.Key));
                            break;

                        case DataType.RawData:
                            WriteBuffer(stream, ExactConvert.ToString(kvp.Key).ToUtf16(), false);
                            break;

                        default:
                            throw new InvalidOperationException("Invalid dictionary key type.");
                        }
                        break;

                    default:
                        throw new InvalidOperationException("Invalid dictionary type.");
                    }
                }
                stream.WriteByte((byte)DataType.End);
            }