forked from jorgeuriarte/grails-i18n-fields-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathI18nFieldsGrailsPlugin.groovy
81 lines (70 loc) · 2.8 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
71
72
73
74
75
76
77
78
79
80
import org.codehaus.groovy.grails.commons.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import i18nfields.*
import grails.util.GrailsUtil
import org.codehaus.groovy.grails.orm.hibernate.HibernateEventListeners
import org.hibernate.event.PostInsertEvent
import grails.util.Holders
class I18nFieldsGrailsPlugin {
static final Logger log = LoggerFactory.getLogger(this)
def version = "1.0.12"
def groupId = "com.ticketbis"
def grailsVersion = "2.4.0 > *"
def pluginExcludes = [
"lib/*",
"grails-app/i18n/*",
"grails-app/domain/i18nfields/*",
"grails-app/controllers/i18nfields/*",
"grails-app/services/i18nfields/*",
"grails-app/taglib/i18nfields/*",
"grails-app/views/*",
"grails-app/views/layouts/*",
"web-app/css/*",
"web-app/images/*",
"web-app/images/skin/*",
"web-app/js/*",
"web-app/js/prototype/*",
]
def dependsOn = [:]
def author = "Jorge Uriarte"
def authorEmail = "[email protected]"
def title = "i18n Fields"
def description = "This plugin provides an easy way of declarativily localize database fields of your content tables."
def documentation = "http://grails.org/plugin/i18n-fields"
def grailsApplication
def setApplication(app) {
log.info "Grails Application injected"
grailsApplication = app
}
def doWithDynamicMethods = { context ->
log.info "Plugin version: $version"
['domain', 'controller', 'service', 'bootstrap'].each {
log.info "Adding 'withLocale' method to '${it}' classes"
application."${it}Classes".each { theClass ->
theClass.metaClass.withLocale = I18nFieldsHelper.withLocale
}
}
['domain'].each {
log.info "Adding saveLocale method to ${it} classes"
application."${it}Classes".each { theClass ->
theClass.metaClass.saveLocale = I18nFieldsHelper.saveLocale
}
}
i18nfields.I18nFieldsHelper.metaClass.getApplicationContext = { -> context }
}
def doWithSpring = {
}
def doWithApplicationContext = { applicationContext ->
def listeners = applicationContext.sessionFactory.eventListeners
def listener = new I18nFieldsListener()
['saveOrUpdate', 'preDelete'].each { type ->
def typeProperty = "${type}EventListeners"
def typeListeners = listeners."${typeProperty}"
def expandedTypeListeners = new Object[typeListeners.length + 1]
System.arraycopy(typeListeners, 0, expandedTypeListeners, 0, typeListeners.length)
expandedTypeListeners[-1] = listener
listeners."${typeProperty}" = expandedTypeListeners
}
}
}