default key generated?

Jul 31, 2012 at 9:39 AM
Edited Jul 31, 2012 at 9:42 AM

Is there any syntax I can use in my Xaml to set the default key being generated?

for instance

<TextBox x:name="helloworld" text="hejsan världen"/>

 

gives me today a resource file looking like

 

Key 			Text
hejsanvärlden 		hejsan världen

 


I want:

 

Key		Text
helloworld 	hejsan världen
Coordinator
Jul 31, 2012 at 10:45 AM

There is no such possibility right now.

If you wish, I can give you some hints how it can be implemented.

Jul 31, 2012 at 1:48 PM
Edited Jul 31, 2012 at 1:50 PM

I solved this with a quick and dirty for now... 

modified the GetNewKey method and checked the XmlNode's parent for a elements attribute equal to "Name", see code below

 

private void GenerateKeys()
        {
            newKeys.Clear();
            
            foreach (var item in Items)
            {
                if (String.IsNullOrEmpty(item.Key) || IsKeyInUse(item.Key))
                {
                    string key = xamlExtractor.GetWellKnownStringKey(item.Text);
                    item.Key = !String.IsNullOrEmpty(key) ? key : GetNewKey(item.ParseItem.Node, item.Text); // GetNewKey(item.Text);


                }
                else
                {
                    if (!IsFullKey(item.Key))
                    {
                        newKeys.Add(item.Key, null);
                    }
                }
            }
        }
             


        private string GetNewKey(XmlNode s, string fallbackValue)
        {
            var sb = new StringBuilder();

            string keyvalue = fallbackValue;
            try
            {
                if (s is XmlAttribute)
                {
                    foreach (XmlNode item in (((XmlAttribute)s).OwnerElement.Attributes))
                    {
                        if ( item.LocalName == "Name" )
                             keyvalue = item.Value;
                    }
                }            
                
            }
            catch
            {

            }

            if (keyvalue == fallbackValue)
            {                
                // Capitalize first letter and after a space
                bool capitalize = true;
                for (int i = 0; i < keyvalue.Length; i++)
                {
                    if (sb.Length == 0 && Char.IsDigit(keyvalue, i))
                    {
                        sb.Append('S');
                    }
                    if (Char.IsLetterOrDigit(keyvalue, i))
                    {
                        if (capitalize)
                        {
                            sb.Append(Char.ToUpper(keyvalue[i]));
                            capitalize = false;
                        }
                        else
                        {
                            sb.Append(keyvalue[i]);
                        }
                    }
                    else if (char.IsWhiteSpace(keyvalue, i))
                    {
                        capitalize = true;
                    }                
                }
            }
            else
            {
                sb.Append(keyvalue);
            }

            string rawKey = sb.ToString().TrimEnd('_');
            if (rawKey.Length > 30)
            {
                rawKey = rawKey.Substring(0, 30);
            }

            string key = rawKey;
            int ii = 1;
            while (IsKeyInUse(key))
            {
                key = rawKey + ii++;
            }
            newKeys.Add(key, null);
            return key;
        }

 

Coordinator
Jul 31, 2012 at 2:37 PM
Edited Jul 31, 2012 at 2:38 PM

Perhaps you should add also support for XmlText node types, to support direct content, like e.g. <Button Name="but1">Push me</Button>

if (s is XmlText)
{
    foreach (XmlNode item in (((XmlText)s).ParentNode.Attributes))
                    {
                        if ( item.LocalName == "Name" )
                             keyvalue = item.Value;
                    }
                }            

Aug 1, 2012 at 5:37 AM

cheers skiba_k!

great addin btw :)