예제 #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());
        }
예제 #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());
        }
예제 #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));
            }
        }
예제 #5
0
        public string GetLocalised(LocalisationParameters parameters)
        {
            if (parameters.Store == null)
            {
                return(ToString());
            }

            return(Value.ToString(Format, parameters.Store.EffectiveCulture));
        }
예제 #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);
            }
        }
예제 #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);
            }
        }
예제 #8
0
 public string GetLocalised(LocalisationParameters parameters) => FormatString(Format, Args, parameters);
예제 #9
0
 public string GetLocalised(LocalisationParameters parameters) => GetPreferred(parameters.PreferOriginalScript);
 protected void ChangeSettings(LocalisationParameters parameters) => currentParameters.Value = parameters;
예제 #11
0
 protected override string FormatString(string fallback, object?[] args, LocalisationParameters parameters)
 => base.FormatString(parameters.Store?.Get(Key) ?? fallback, args, parameters);