简体   繁体   中英

How to prevent long strings from being written to resx file

Even though my form is not set to be localized, Visual Designer puts text into the .resx instead of directly in the code if I have a label whose text is over 200 characters long.

This is problem because I am using a custom localization approach that pulls localizable strings from the code, but it doesn't search resx files.

Is there a way to prevent this behavior? I can just hard-code the string in my code so the extractor can get it, but this is annoying because the text looks wrong in Designer. Alternatively, I could use multiple labels, but that just feels wrong.

I've seen other people asking a similar questions in other forums, but no answers.

I don't have a complete solution, but it is possible to scan the resources associated with a type and find the strings that the designer has decided to do this to.

            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
                    }
                }
            }

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