3 Usage - Reference Documentation
Authors: Søren Berg Glasius
Version: 1.0.8
3 Usage
Basic usage
You just annotate your ennumeration with @I18nEnumpackage test@I18nEnum
enum MyEnum {
ONE, Two, three
}
test.MyEnum.ONE.codes == ['test.MyEnum.ONE', 'test.MyEnum.ONE', 'test.MyEnum.one'] test.MyEnum.Two.codes == ['test.MyEnum.TWO', 'test.MyEnum.Two', 'test.MyEnum.two'] test.MyEnum.three.codes == ['test.MyEnum.THREE', 'test.MyEnum.three', 'test.MyEnum.three']
test.MyEnum.ONE.defaultMessage == 'ONE' test.MyEnum.Two.defaultMessage == 'Two' test.MyEnum.three.defaultMessage == 'three'
Advanced usage
To get even more control over, how the codes and the default value are generated, you can add parameters to the annotation:@I18nEnum(prefix = '', postfix = '', shortName = false, defaultNameCase = DefaultNameCase.UNCHANGED)
Prefix and Postfix
Prefix controls what is added before the package name. If you do not add a dot after the prefix, one will be added for you.Postfix controls what is added after the ENUM_VALUE. If you do not add a dot before the postfix, one will be added for you.Example:
package test@I18nEnum(prefix = 'enum', postfix = 'label')
enum MyEnum {
ONE, Two, three
}
test.MyEnum.ONE.codes == ['enum.test.MyEnum.ONE.label', 'enum.test.MyEnum.ONE.label', 'enum.test.MyEnum.one.label'] test.MyEnum.Two.codes == ['enum.test.MyEnum.TWO.label', 'enum.test.MyEnum.Two.label', 'enum.test.MyEnum.two.label'] test.MyEnum.three.codes == ['enum.test.MyEnum.THREE.label', 'enum.test.MyEnum.three.label', 'enum.test.MyEnum.three.label']
shortName
If you do not want the package name in the generated codes, just add shortName = true to your annotation parametersExample:
package test@I18nEnum(shortName = true) enum MyEnum { ONE, Two, three }
test.MyEnum.ONE.codes == ['MyEnum.ONE', 'MyEnum.ONE', 'MyEnum.one'] test.MyEnum.Two.codes == ['MyEnum.TWO', 'MyEnum.Two', 'MyEnum.two'] test.MyEnum.three.codes == ['MyEnum.THREE', 'MyEnum.three', 'MyEnum.three']
defaultNameCase
To control the defaultMessage you can use defaultNameCase. It can be set to one of the following values:UPPER_CASE, LOWER_CASE, CAPITALIZE, ALL_CAPS, UNCHANGED
Example:
DefaultNameCase.CAPITALIZE
package test@I18nEnum(defaultNameCase = DefaultNameCase.CAPITALIZE)
enum MyEnum {
ONE, Two, three
}
test.MyEnum.ONE.defaultMessage == 'One' test.MyEnum.Two.defaultMessage == 'Two' test.MyEnum.three.defaultMessage == 'Three'
DefaultNameCase.ALL_CAPS
@I18nEnum(defaultNameCase = DefaultNameCase.ALL_CAPS) enum MyEnum { ONE, Two, three, Four_five }
test.MyEnum.ONE.defaultMessage == 'One' test.MyEnum.Two.defaultMessage == 'Two' test.MyEnum.three.defaultMessage == 'Three' test.MyEnum.Four_five.defaultMessage == 'Four Five'
DefaultNameCase.LOWER_CASE
package test@I18nEnum(defaultNameCase = DefaultNameCase.LOWER_CASE)
enum MyEnum {
ONE, Two, three
}
test.MyEnum.ONE.defaultMessage == 'one' test.MyEnum.Two.defaultMessage == 'two' test.MyEnum.three.defaultMessage == 'three'
Configuration
It is possible to configure default settings from configuration.In Configuration.groovy create a configuration tree like this:import grails.plugin.i18nEnum.transformation.DefaultNameCase// Your other configurationgrails { plugin { i18nEnum { postfix = "label" shortName = true defaultNameCase = DefaultNameCase.ALL_CAPS } } }