Added test functionality (getting list of bundles with translations).

This commit is contained in:
Prominence 2016-06-23 16:09:19 +03:00 committed by Prominence
parent 9d7c146f99
commit fe06a6d5d3
21 changed files with 131 additions and 3 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@
.project
target
.idea
*.zip
*.zip
.slcache

View File

@ -16,6 +16,8 @@ grails.project.fork = [
console: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256]
]
grails.server.port.http = 8082
grails.project.dependency.resolver = "maven" // or ivy
grails.project.dependency.resolution = {
// inherit Grails' default dependencies
@ -40,9 +42,11 @@ grails.project.dependency.resolution = {
}
plugins {
build(":release:3.0.1",
":rest-client-builder:1.0.3") {
build(':release:3.0.1',
':rest-client-builder:1.0.3') {
export = false
}
build(':tomcat:7.0.55')
}
}

View File

@ -20,3 +20,6 @@ log4j = {
'org.hibernate',
'net.sf.ehcache.hibernate'
}
grails.views.gsp.encoding = "UTF-8"
grails.converters.encoding = "UTF-8"

View File

@ -0,0 +1,13 @@
package by.prominence.translations
class TranslationsOverviewController {
def bundleService
def index() { }
def showBundles() {
render view: 'showBundles', model: [bundles: bundleService.getBundles()]
}
}

View File

@ -0,0 +1,11 @@
package by.prominence.translations
class Bundle {
String name
List<File> propertiesList = new ArrayList<>()
public void addPropertyFile(File file) {
propertiesList.add(file)
}
}

View File

@ -0,0 +1,8 @@
package by.prominence.translations
class BundleProperties {
Bundle bundle
Properties properties
}

View File

View File

@ -0,0 +1 @@
test.message = Test message here

View File

@ -0,0 +1 @@
test.message = Test message here

View File

@ -0,0 +1 @@
test.message = Test message here

View File

@ -0,0 +1 @@
test.message = Тестовое сообщение

View File

@ -0,0 +1,49 @@
package by.prominence.translations
class BundleService {
private HashSet<Bundle> cachedBundles
private static final defaultFolder = "i18n"
HashSet<Bundle> getBundles(boolean noCache = false) {
if (!cachedBundles || noCache) {
cachedBundles = searchForBundles().values()
}
return cachedBundles
}
private Map<String, Bundle> searchForBundles() {
Map<String, Bundle> result = new HashMap<>()
new File('.').eachFileRecurse() { File file ->
if(file.name.endsWith('.properties')) {
if (isTranslationProperty(file)) {
String bundleName = getBundleName(file.name)
if (!result.get(bundleName)) {
Bundle bundle = new Bundle(name: bundleName)
result.put(bundleName, bundle)
}
result.get(bundleName).addPropertyFile(file)
}
}
}
return result
}
private boolean isTranslationProperty(File file) {
return file.parent.contains(defaultFolder)
}
private String getFileNameWithoutExtension(String filename) {
return filename.substring(0, filename.lastIndexOf('.'))
}
private String getBundleName(String filename) {
return filename.indexOf('_') < 0 ?
getFileNameWithoutExtension(filename) :
filename.substring(0, filename.indexOf('_'))
}
}

View File

@ -0,0 +1,11 @@
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Test</title>
</head>
<body>
<g:message code="test.message" />
</body>
</html>

View File

@ -0,0 +1,24 @@
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>bundles</title>
</head>
<body>
<g:if test="${bundles}">
<g:each in="${bundles}" var="bundle">
${bundle.name} <br />
<g:each in="${bundle.propertiesList}" var="properties">
-> ${properties.name} <br />
${properties.text.eachLine { line ->
print('----------> ' + line + '<br/>')
}}
</g:each>
</g:each>
</g:if>
<g:else>
There is no bundles!
</g:else>
</body>
</html>