Exemplo n.º 1
0
        protected virtual string FormatString(string format, object?[] args, LocalisationParameters parameters)
        {
            if (parameters.Store == null)
            {
                return(ToString());
            }

            try
            {
                return(string.Format(parameters.Store.EffectiveCulture, format, args.Select(argument =>
                {
                    if (argument is LocalisableString localisableString)
                    {
                        argument = localisableString.Data;
                    }

                    if (argument is ILocalisableStringData localisableData)
                    {
                        return localisableData.GetLocalised(parameters);
                    }

                    return argument;
                }).ToArray()));
            }
            catch (FormatException e)
            {
                // The formatting has failed
                Logger.Log($"Localised format failed. Culture: {parameters.Store.EffectiveCulture}, format string: \"{format}\", base format string: \"{Format}\". Exception: {e}",
                           LoggingTarget.Runtime, LogLevel.Verbose);
            }

            return(ToString());
        }
Exemplo n.º 2
0
        public string GetLocalised(LocalisationParameters parameters)
        {
            if (parameters.Store == null)
            {
                return(ToString());
            }

            string localisedFormat = parameters.Store.Get(Key) ?? Fallback;

            try
            {
                return(string.Format(parameters.Store.EffectiveCulture, localisedFormat, Args.Select(argument =>
                {
                    if (argument is LocalisableString localisableString)
                    {
                        argument = localisableString.Data;
                    }

                    if (argument is ILocalisableStringData localisableData)
                    {
                        return localisableData.GetLocalised(parameters);
                    }

                    return argument;
                }).ToArray()));
            }
            catch (FormatException e)
            {
                // The formatting has failed
                Logger.Log($"Localised format failed. Key: {Key}, culture: {parameters.Store.EffectiveCulture}, fallback format string: \"{Fallback}\", localised format string: \"{localisedFormat}\". Exception: {e}",
                           LoggingTarget.Runtime, LogLevel.Verbose);
            }

            return(ToString());
        }
        public string GetLocalised(LocalisationParameters parameters)
        {
            if (parameters.Store == null)
            {
                return(ToString());
            }

            var localisedFormat = parameters.Store.Get(Key);

            if (localisedFormat == null)
            {
                return(ToString());
            }

            try
            {
                return(string.Format(parameters.Store.EffectiveCulture, localisedFormat, Args));
            }
            catch (FormatException e)
            {
                // The formatting has failed
                Logger.Log($"Localised format failed. Key: {Key}, culture: {parameters.Store.EffectiveCulture}, fallback format string: \"{Fallback}\", localised format string: \"{localisedFormat}\". Exception: {e}",
                           LoggingTarget.Runtime, LogLevel.Verbose);
            }

            return(ToString());
        }
Exemplo n.º 4
0
        public string GetLocalised(LocalisationParameters parameters)
        {
            string stringData  = getStringData(parameters);
            var    cultureText = parameters.Store?.EffectiveCulture?.TextInfo ?? CultureInfo.InvariantCulture.TextInfo;

            switch (Casing)
            {
            case Casing.UpperCase:
                return(cultureText.ToUpper(stringData));

            case Casing.TitleCase:
                return(cultureText.ToTitleCase(stringData));

            case Casing.LowerCase:
                return(cultureText.ToLower(stringData));

            case Casing.SentenceCase:
                return(toSentenceCase(stringData, cultureText));

            case Casing.Default:
            default:
                return(stringData);
            }

            // taken from https://github.com/Humanizr/Humanizer/blob/606e958cb83afc9be5b36716ac40d4daa9fa73a7/src/Humanizer/Transformer/ToSentenceCase.cs#L12-L22
            string toSentenceCase(string input, TextInfo textInfo)
            {
                if (input.Length >= 1)
                {
                    return(textInfo.ToUpper(input[0]) + input.Substring(1));
                }

                return(textInfo.ToUpper(input));
            }
        }
Exemplo n.º 5
0
        public string GetLocalised(LocalisationParameters parameters)
        {
            if (parameters.Store == null)
            {
                return(ToString());
            }

            return(Value.ToString(Format, parameters.Store.EffectiveCulture));
        }
Exemplo n.º 6
0
        private string getStringData(LocalisationParameters localisationParameters)
        {
            switch (String.Data)
            {
            case string plain:
                return(plain);

            case ILocalisableStringData data:
                return(data.GetLocalised(localisationParameters));

            default:
                return(string.Empty);
            }
        }
Exemplo n.º 7
0
        public string GetLocalised(LocalisationParameters parameters)
        {
            var stringData  = getStringData(parameters);
            var cultureText = parameters.Store?.EffectiveCulture?.TextInfo ?? CultureInfo.InvariantCulture.TextInfo;

            switch (Casing)
            {
            case Casing.UpperCase:
                return(cultureText.ToUpper(stringData));

            case Casing.TitleCase:
                return(cultureText.ToTitleCase(stringData));

            case Casing.LowerCase:
                return(cultureText.ToLower(stringData));

            case Casing.Default:
            default:
                return(stringData);
            }
        }
Exemplo n.º 8
0
 public string GetLocalised(LocalisationParameters parameters) => FormatString(Format, Args, parameters);
Exemplo n.º 9
0
 public string GetLocalised(LocalisationParameters parameters) => GetPreferred(parameters.PreferOriginalScript);
 protected void ChangeSettings(LocalisationParameters parameters) => currentParameters.Value = parameters;
Exemplo n.º 11
0
 protected override string FormatString(string fallback, object?[] args, LocalisationParameters parameters)
 => base.FormatString(parameters.Store?.Get(Key) ?? fallback, args, parameters);