-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI18nFieldsGrailsPlugin.groovy
70 lines (59 loc) · 2.57 KB
/
I18nFieldsGrailsPlugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import org.codehaus.groovy.grails.commons.GrailsClassUtils
import org.codehaus.groovy.grails.commons.metaclass.*
import org.codehaus.groovy.grails.commons.*
class I18nFieldsGrailsPlugin {
// the plugin version
def version = "0.4"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "1.3 > *"
// the other plugins this plugin depends on
def dependsOn = [:]
// resources that are excluded from plugin packaging
def pluginExcludes = [
"grails-app/views/error.gsp"
]
def config = ConfigurationHolder.config
// TODO Fill in these fields
def author = "Jorge Uriarte, Taioli Fabiano"
def authorEmail = "[email protected], [email protected]"
def title = "i18n Fields"
def description = '''\\
This plugin provide an easy way of declarativily localize database fields of your content tables.
'''
// URL to the plugin's documentation
def documentation = "http://grails.org/plugin/i18n-fields"
def getField = { fieldName ->
delegate."${fieldName}_${i18nfields.I18nFieldsHelper.getLocale().language}"
}
def setField = { fieldName, value ->
delegate."${fieldName}_${i18nfields.I18nFieldsHelper.getLocale().language}" = value
}
def withLocale = { newLocale, Closure clos ->
def previous = i18nfields.I18nFieldsHelper.getLocale()
i18nfields.I18nFieldsHelper.setLocale(newLocale)
clos.run()
i18nfields.I18nFieldsHelper.setLocale(previous)
}
def doWithDynamicMethods = { ctx ->
// TODO: getLang()
// TODO: getI18nFieldValue(field)
// TODO: setI18nFieldValue(field, value)
MetaClassRegistry registry = GroovySystem.metaClassRegistry
application.domainClasses.each {domainClass ->
def i18n_fields = GrailsClassUtils.getStaticPropertyValue(domainClass.clazz, "i18n_fields" )
def i18n_langs = config.i18nFields.i18n_langs
if (i18n_fields && i18n_langs) {
i18n_fields.each() { f ->
def getter = GrailsClassUtils.getGetterName(f)
def setter = GrailsClassUtils.getSetterName(f)
println "Adding ${getter}"
domainClass.metaClass."${getter}" = getField.curry(f)
domainClass.metaClass."${setter}" = setField.curry(f)
}
}
}
application.allArtefacts.each { theClass ->
theClass.metaClass.withLocale = withLocale
}
}
}