简体   繁体   中英

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] .

Which Unicode character can I use for this purpose? Ideally, it would be non-printable, supported in JS and 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.

But in case you need to embed such information into a string, then you could use 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.

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. As described in 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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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