【问题标题】:Download a file on the basis of language (Current Culture)根据语言(当前文化)下载文件
【发布时间】:2013-07-04 10:23:35
【问题描述】:

我正在开发一个 Windows 应用程序,它使用 WebClient 从 http URL 下载模板。

我必须在报告中实现本地化。应用程序将始终使用默认语言(英语),并且我将有一个变量来保存必须打印报告的语言。报告模板将根据语言下载。具有命名约定的每种语言的报告模板都不同,例如

默认:http://www.xyz.com/report/report.htm

英文:report.en-US.htm, 西班牙语:report.es-ES.htm 葡萄牙语:report.pt-PT.htm

我是否对每种语言都使用 switch..case 或任何其他方式。

【问题讨论】:

    标签: c# .net globalization


    【解决方案1】:

    如果文件名格式保持不变,您可以这样做

    string cultureCode = "en-US"; //set current locale    
    Uri reportUri = new Uri(String.Format("http://www.xyz.com/report/report.{0}.htm", cultureCode), UriKind.Absolute);
    

    这样,它会为您动态创建 URI,并添加相关的语言环境。作为一种快速的方法

    public void Uri GetLocalReportUri(string cultureCode)
    {  
       return new Uri(String.Format("http://www.xyz.com/report/report.{0}.htm", cultureCode), UriKind.Absolute);
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 Globalization 类来获取您当前的文化。试试下面的。

      string culture = System.Globalization.CultureInfo.CurrentCulture.Name;
      
      string destUri = String.Format("http://www.xyz.com/report/report.{0}.htm", culture);
      

      【讨论】:

        【解决方案3】:

        您可以从

        获取当前的文化名称

        System.Globalization.CultureInfo.CurrentUICulture.Name

        System.Web.HttpRequest.UserLanguages

        所以你可以按照以下方式做一些事情

        string url = string.Format("http://www.xyz.com/report/report{0}.htm", CultureInfo.CurrentUICulture.Name);

        输出类似

        http://www.xyz.com/report/report.en-US.htm

        【讨论】:

          猜你喜欢
          • 2021-08-28
          • 1970-01-01
          • 2015-01-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多