using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Web;
using System.Web.UI;

/// <summary>
/// Summary description for i18n
/// </summary>
public static class i18n
{
    private static string GetInternalRequestBaseUrl()
    {
        if (string.Equals(ConfigurationManager.AppSettings["IHBOX_CONTAINER_LOCAL_IIS_REQUESTS"], "true", StringComparison.OrdinalIgnoreCase))
        {
            return "http://localhost";
        }

        return "http://"
            + HttpContext.Current.Request.Url.Host
            + ":" + HttpContext.Current.Request.Url.Port;
    }

    public static string GetJedLocaleData(string siteLang)
    {
        WebClient client = new WebClient();
        string text = "";
        try
        {
            string url = GetInternalRequestBaseUrl()
                + ((Page)HttpContext.Current.Handler).ResolveUrl("~/app/i18n/resx2json.aspx?path=dataLocalization&customPath=app/i18n&appendASPXExt=false");

            Stream stream = client.OpenRead(url);
            StreamReader reader = new StreamReader(stream);
            text = reader.ReadToEnd();
        }
        catch
        {
            text = "{ 'messages': { '': { 'domain': 'messages', 'lang':'en', 'plural_forms':'' } } }";
        }

        return text; 
    }
}
