是添加字符串用占位符(`{0}`)资源化是一个好主意? [英] Is adding strings with placeholders (`{0}`) into resources a good idea?

查看:219
本文介绍了是添加字符串用占位符(`{0}`)资源化是一个好主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个字符串转换成资源文件。我的应用程序将被本地化。结果
但是,是增加与占位符字符串( {0} )的资源化是一个好主意?结果
就恰如如果一些非技术人员做本地化?有没有为他搞砸了,不知不觉的方式?



如果这不是一个好主意,我应该怎么办?



下面是简单的例子。我将使用WPF资源字典。



示例:



  // Resource1.resx 
//名称|价值
// ------------------------------------------- --------------------
// RELATIONSHIP_STATUS_MSG | {0}与{1}的关系。
//


类节目
{
静态无效的主要(字串[] args)
{
弦乐味精=的String.Format(Resource1.RELATIONSHIP_STATUS_MSG,
罗密欧,Juliot);
Console.WriteLine(MSG);
}
}


解决方案

好我认为这是一个好主意,因为这是渲染参数化和本地化字符串的一个简单,快捷的方式。



顺便说一句,当你在你的问题,非-tech人可以打破你的本地化字符串,因为他们不明白什么是{0}。我已经为解决这个问题两个办法




  1. 只需注意非技术人员维护本地化的字符串,他们不得照顾大约在括号中的文字


  2. 使用指定的占位符:{一些标识符},只是使用 someTextResource.Replace( {一些标识符},someTextVar)




关于第二一个,你可以实现一些方法接受一个的IDictionary< TKEY的,TValue方式> 替换关系,这里的关键是要更换标识,和价值将更换标识的文字实例


I have added a string into a resources file. My application will be localized.
But, is adding strings with placeholders ({0}) into resources a good idea?
What if some non-technical person does localization? Is there a way for him to screw it up, unknowingly?

If this isn't a good idea, what should I do?

Here is simple example. I will be using WPF resource dictionaries.

Example:

// Resource1.resx
//        Name               |            Value
//---------------------------------------------------------------
// RELATIONSHIP_STATUS_MSG   | {0} is in relationship with {1}. 
//


class Program
{
    static void Main(string[] args)
    {
        string msg = string.Format(Resource1.RELATIONSHIP_STATUS_MSG, 
                                   "Romeo", "Juliot");
        Console.WriteLine(msg);
    }
}

解决方案

Well, I believe that it's a good idea because this is an easy and quick way of rendering parametrized and localized strings.

By the way, as you say in your question, non-tech people can break your localization strings because they don't understand what's "{0}". I've two "approaches" for solving that problem

  1. Just notice non-tech people maintaining localized strings that they mustn't take care about text in brackets.

  2. Use named placeholders: "{some-identifier}" and just use someTextResource.Replace("{some-identifier}", someTextVar).

About 2nd one, you can implement some method accepting an IDictionary<TKey, TValue> instance of substitution relations, where the key is the identifier to replace, and value the text to put replacing the identifier.

这篇关于是添加字符串用占位符(`{0}`)资源化是一个好主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆