【问题标题】:How to prevent long strings from being written to resx file如何防止将长字符串写入 resx 文件
【发布时间】:2013-02-08 12:52:14
【问题描述】:

即使我的表单未设置为本地化,如果我有一个文本长度超过 200 个字符的标签,Visual Designer 会将文本放入 .resx 中,而不是直接放入代码中。

这是一个问题,因为我使用了一种自定义本地化方法,该方法从代码中提取可本地化的字符串,但它不搜索 resx 文件。

有没有办法防止这种行为?我可以在我的代码中对字符串进行硬编码,以便提取器可以获取它,但这很烦人,因为文本在 Designer 中看起来是错误的。或者,我可以使用多个标签,但这感觉不对。

我在其他论坛看到其他人问过类似的问题,但没有答案。

【问题讨论】:

    标签: c# visual-studio designer resx


    【解决方案1】:

    我没有完整的解决方案,但可以扫描与类型关联的资源并找到设计者决定这样做的字符串。

                var resources = new ResourceManager(type);
                using (var set = resources.GetResourceSet(CultureInfo.InvariantCulture, true, false))
                {
                    if (set != null)
                    {
                        foreach (DictionaryEntry res in set)
                        {
                            var key = res.Key as string;
                            var val = res.Value as string;
                            if (key == null || val == null)
                                continue;
                            if (!key.EndsWith(".Text"))
                                continue;
                            key = key.Substring(0, key.Length - ".Text".Length);
                            // key is now the name of your control
                            // val is the string the designer stored as its Text in the default resource
                        }
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 2016-12-09
      • 1970-01-01
      • 2010-12-24
      • 2021-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多