attributes

Jul 20, 2015 at 10:17 AM
I will be really interested in having atributes to be parsed for localization. is it a difficult feature to implement?
Coordinator
Jul 20, 2015 at 4:43 PM
Hi,

attributes are omitted by design. The reason is that whatever is passed as a parameter to an attribute, must be a compile-time constant - it therefore cannot be replaced with a reference to resources. Do you have a concrete example of that behavior not being desirable?
Implementing such functionality would not be entirely trivial - although it would be quite easy to detect the strings, the question is, what should they be replaced with? In the current model, a pair (resource, key) is used and as mentioned above, that would render the code uncompilable.

Regards

cre8or
Jul 21, 2015 at 6:36 AM
I see. if i had an attribute with a localizable string as a parameter (say [Text("Localizable String")]) i would like to generate a "Localizable String" string resource named "locString" and substitute "Localizable String" with "locString". I'm not completely sure but i thought this was the common pattern for such cases.

Regards

avc
Coordinator
Jul 21, 2015 at 7:45 AM
Hi,

as I said, a parameter of an attribute must be "a constant expression, typeof expression or array creation expression of an attribute parameter type". Reference to resources is neither, since it is implemented as a property getter. Try it yourself - replace a string in an attribute with a reference to resources and you should get a compilation error.

Regards,

cre8or
Jul 21, 2015 at 8:02 AM
yes i know. but the string resource name, if i am not mistaken, (in the example "locString") is a constant expression.
the transformation would be

[Text("Localized String")]
class MyClass

to

[Text("LocalizedStringResourceGeneratedByVisualLocalizerName")]
class MyClass

Obviously some loading-string-resource code is needed when reading the attribute.

Regards

avc
Coordinator
Jul 21, 2015 at 8:17 AM
Ok I see. I'll see what I can do, I'm quite busy at the moment. You're of course welcome to download the code and make the changes yourself.

Regards,

cre8or
Jul 21, 2015 at 8:46 AM
Thank you cre8or, if you agree i will try to make the changes after holydays( so september ;)). just give me a pointer to the .cs to modify

Regards

avc
Coordinator
Jul 26, 2015 at 9:16 AM
Hi guys,
I hit into this limitation in another project, you can check what I did
https://commandlineparser.codeplex.com/wikipage?title=Using%20resource%20files%20for%20argument%20descriptions&referringTitle=Home

Essentially, the attribute value is used as a constant to look up the resource. However, when accessing the attribute value, you need an additional postprocessing step to lookup the resource and replace the constant with the localized string, as this is not done by the reflection runtime.