【问题标题】:Proper way to initialize gettext in runtime in asp.net / monodevelop在 asp.net/monodevelop 运行时初始化 gettext 的正确方法
【发布时间】:2015-12-22 22:11:35
【问题描述】:

我正在尝试在 mac 上使用 monodevelop 进行本地化以在 asp.net mvc 项目中工作。我添加了一个翻译项目,并用丹麦语翻译了文本“欢迎”。

public class HomeController : Controller
{
    public ActionResult Index ()
    {
        var culture = CultureInfo.CreateSpecificCulture("da");      
        System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
        System.Threading.Thread.CurrentThread.CurrentCulture = culture;
        Mono.Unix.Catalog.Init("i8n1", "./locale");
        ViewData ["Message"] = Mono.Unix.Catalog.GetString("Welcome");
        return View ();
    }
}

但是文本没有被翻译。 有什么想法吗?

【问题讨论】:

  • 我在 Ubuntu 上遇到了同样的问题。

标签: asp.net-mvc mono gettext


【解决方案1】:

您需要 locale 文件夹的完整路径。

MonoDevelop 做了这样的事情(为了简洁而编辑)

string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
location = Path.GetDirectoryName(location);

string catalogPath = Path.Combine (location, "locale");

Catalog.Init ("monodevelop", catalogPath);

【讨论】:

    【解决方案2】:

    Mono.Unix.Catalog 不适合 ASP.NET。它使用“每环境”方法,而对于 ASP.NET,您需要“每线程”方法。

    这个库绝对值得一看 http://sourceforge.net/p/gettextnet/

    供参考:http://lists.ximian.com/pipermail/mono-devel-list/2008-March/027174.html

    【讨论】:

      【解决方案3】:

      答案在这里:http://mono.1490590.n4.nabble.com/Mono-Unix-Catalog-Init-where-does-it-get-the-locale-from-td1532586.html

      public static void Main (string[] args)
          {
              var culture = CultureInfo.CreateSpecificCulture ("de");
              Thread.CurrentThread.CurrentCulture = culture;
              Environment.SetEnvironmentVariable ("LANGUAGE", "de_DE"); 
              Catalog.Init ("i8n1", "./locale");
      
              Console.WriteLine (Catalog.GetString("Hello World!"));
          }
      

      它适用于我在 Ubuntu/Mono 上。感谢 Vladimir 提出的好问题和 Jonathan 的出色回答。

      【讨论】:

        猜你喜欢
        • 2019-10-24
        • 2019-10-03
        • 2023-03-28
        • 2018-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-24
        相关资源
        最近更新 更多