Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WithCulture and GetAllStrings #50

Open
suatsuphi opened this issue May 6, 2018 · 1 comment
Open

WithCulture and GetAllStrings #50

suatsuphi opened this issue May 6, 2018 · 1 comment
Labels

Comments

@suatsuphi
Copy link

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;
        }
    }

    

}

}

@damienbod damienbod added the bug label May 6, 2018
@suatsuphi
Copy link
Author

could you fix this
thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants