damienbod / AspNetCoreLocalization

Localization.SqlLocalizer & ASP.NET Core MVC Localization Examples
http://damienbod.com/2015/10/21/asp-net-5-mvc-6-localization/
MIT License
251 stars 101 forks source link

WithCulture and GetAllStrings #50

Open suatsuphi opened 6 years ago

suatsuphi commented 6 years ago

Hi, can you fix/add WithCulture and GetAllStrings

using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using Microsoft.Extensions.Localization;

namespace Localization.SqlLocalizer.DbStringLocalizer { public class SqlStringLocalizer : IStringLocalizer {

    private readonly Dictionary<string, string> _localizations;

    private readonly DevelopmentSetup _developmentSetup;
    private readonly string _resourceKey;
    private bool _returnKeyOnlyIfNotFound;
    private bool _createNewRecordWhenLocalisedStringDoesNotExist;

    private CultureInfo _currentCulture;

    public SqlStringLocalizer(Dictionary<string, string> localizations, 
        DevelopmentSetup developmentSetup, 
        string resourceKey, bool returnKeyOnlyIfNotFound, 
        bool createNewRecordWhenLocalisedStringDoesNotExist,
        CultureInfo culture = null)
    {
        _localizations = localizations;
        _developmentSetup = developmentSetup;
        _resourceKey = resourceKey;
        _returnKeyOnlyIfNotFound = returnKeyOnlyIfNotFound;
        _createNewRecordWhenLocalisedStringDoesNotExist = createNewRecordWhenLocalisedStringDoesNotExist;
        _currentCulture = culture;
    }
    public LocalizedString this[string name]
    {
        get
        {
            bool notSucceed;
            var text = GetText(name, out notSucceed);

            return new LocalizedString(name, text,notSucceed);
        }
    }

    public LocalizedString this[string name, params object[] arguments]
    {
        get
        {
            return this[name];
        }
    }

    public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
    {
        return _localizations.Select(x => new LocalizedString(x.Key, x.Value));

    }

    public IStringLocalizer WithCulture(CultureInfo culture)
    {
        return new SqlStringLocalizer(_localizations,
         _developmentSetup,
         _resourceKey,  _returnKeyOnlyIfNotFound,
         _createNewRecordWhenLocalisedStringDoesNotExist,
         culture);
    }

    private string GetText(string key,out bool notSucceed)
    {

if NET451

        var _culture = _currentCulture ?? System.Threading.Thread.CurrentThread.CurrentCulture; //var culture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();

elif NET46

        var _culture = _currentCulture ?? System.Threading.Thread.CurrentThread.CurrentCulture; //var culture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();

else

        var _culture = _currentCulture ?? CultureInfo.CurrentCulture; //CultureInfo.CurrentCulture.ToString();

endif

        string culture = _culture.ToString();

        string computedKey = $"{key}.{culture}";

        string result;
        if (_localizations.TryGetValue(computedKey, out result))
        {
            notSucceed = false;
            return result;
        }
        else
        {
            notSucceed = true;
            if (_createNewRecordWhenLocalisedStringDoesNotExist)
            {
                _developmentSetup.AddNewLocalizedItem(key, culture, _resourceKey);
                _localizations.Add(computedKey, computedKey);
                return computedKey;
            }
            if (_returnKeyOnlyIfNotFound)
            {
                return key;
            }

            return _resourceKey + "." + computedKey;
        }
    }

}

}

suatsuphi commented 5 years ago

could you fix this thanks