简体   繁体   English

我可以使用哪个unicode字符来“标记”字符串?

[英]What unicode character I can use to “flag” a string?

I want to represent an object that has several text properties, every one representing the same text value but in different languages. 我想代表一个具有多个文本属性的对象,每个对象代表相同的文本值,但使用不同的语言。 In case the user modifies a single field, the other fields should be revised, and I'm thinking on adding a single Unicode character at the beginning of the string of the other fields, and then to check for fields that need attention, I just have to check the value at obj.text_prop[0] . 如果用户修改单个字段,则应该修改其他字段,而我正在考虑在其他字段的字符串开头添加单个Unicode字符,然后检查是否需要注意的字段,我只是必须检查obj.text_prop[0]的值。

Which Unicode character can I use for this purpose? 我可以为此使用哪个Unicode字符? Ideally, it would be non-printable, supported in JS and JSON. 理想情况下,它将是不可打印的,并且在JS和JSON中受支持。

Such flagging should be done some other way, at a protocol level other than character level. 这种标记应该以其他方式在字符级别以外的协议级别上进行。 For example, consider as making each language version an object rather than just a string; 例如,考虑将每种语言版本作为对象而不是字符串。 the object could then have properties such as needsAttention in addition to the property that contains the string. 这样,除了包含字符串的属性之外,该对象还可以具有诸如needsAttention之类的属性。

But in case you need to embed such information into a string, then you could use ZERO WIDTH SPACE U+200B. 但是,如果您需要将此类信息嵌入到字符串中,则可以使用ZERO WIDTH SPACE U + 200B。 As such it means line break opportunity, but this should not disturb here. 因此,这意味着有换行的机会,但在此不应打扰。 The main problem is probably that old versions of IE may display it as a small rectangle. 主要问题可能是IE的旧版本可能将其显示为一个小矩形。

Alternatively, you could use a noncharacter code point such as U+FFFF, if you can make sure that the string is never sent anywhere from the program without removing this code point. 另外,如果可以确保在不删除该代码点的情况下,从不将字符串从程序中发送到任何地方,则可以使用非字符代码点,例如U + FFFF。 As described in Ch. 如Ch。 16 of the Unicode Standard, Special Areas and Format Characters , noncharacter code points are reserved for internal use in an application and should never be used in text interchange. Unicode标准, 特殊区域和格式字符中的16个非字符代码点保留供应用程序内部使用,切勿在文本交换中使用。

I would suggest you not to use strange characters in the beginning of the line. 我建议您不要在行首使用奇怪的字符。 You can implement something like this: 您可以实现以下内容:

<script type="text/javascript">
    function LocalizationSet(){};

    LocalizationSet.prototype.localizationItems = [];
    LocalizationSet.prototype.itemsNeedAttention = [];

    LocalizationSet.prototype.setLocalization = function(langId, text)
    {
        this.localizationItems[langId] = text;
        this.itemsNeedAttention[langId] = true;
    }

    LocalizationSet.prototype.getLocalization = function(langId)
    {
        return this.localizationItems[langId];
    }


    LocalizationSet.prototype.needsAttention = function(langId)
    {
        if(this.itemsNeedAttention[langId] == null)
        {
            return false;
        }
        return this.itemsNeedAttention[langId];
    }

    LocalizationSet.prototype.unsetAttentionFlags = function()
    {
        for(var it in this.itemsNeedAttention)
        {
            this.itemsNeedAttention[it] = false;
        }
    }

    //Example
    var set = new LocalizationSet();
    set.setLocalization("en","Hello");
    set.setLocalization("de","Willkommen");

    alert(set.needsAttention("en"));
    alert(set.needsAttention("de"));

    set.unsetAttentionFlags();
    alert(set.needsAttention("en"));
    set.setLocalization("en","Hi");
    alert(set.needsAttention("en"));
    //Shows true,true,false,true
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM