How to set default language to English - grails

I've tried to set it in Bootstrap but it don't work
Also tried in ../conf/spring/resources.groovy .. it don't work either.
In Bootstrap i tried with:
Locale defLocale = new Locale("en", "GB");
Locale.setDefault(defLocale);
And in resources.groovy I tried this:
import org.springframework.web.servlet.i18n.SessionLocaleResolver
beans = {
localeResolver(SessionLocaleResolver) {
defaultLocale= new java.util.Locale("en","GB")
}
}
and a lot of variants I found when googling.
I'm sure it must be a way but it seems very hard to find.

When you create a session(for example when logging in), you can do the following:
if(setDefaultLanguage) {
session['org.springframework.web.servlet.i18n.' +
'SessionLocaleResolver.LOCALE'] = 'en-GB'
}

Try this,
String springLocaleAtt = 'org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'
session[springLocaleAtt] = new Locale('en_GB')

Related

Xtext crossreferencing no longer working?

I have used Xtext for many years, and have always been able to cross-reference from one grammar to another grammar. But today, on Eclipse Photon, the usual method no longer works.
In the same workspace, I create two Xtext projects, using default options, org.xtext.example.adsl.ADsl
grammar org.xtext.example.adsl.ADsl with org.eclipse.xtext.common.Terminals
generate aDsl "http://www.xtext.org/example/adsl/ADsl"
AModel:
agreetings+=AGreeting*;
AGreeting:
'AHello' name=ID '!';
and org.xtext.example.bdsl.BDsl,
grammar org.xtext.example.bdsl.BDsl with org.eclipse.xtext.common.Terminals
generate bDsl "http://www.xtext.org/example/bdsl/BDsl"
//import "http://www.xtext.org/example/adsl/ADsl" as adsl
ModelB:
bgreetings+=BGreeting*;
BGreeting:
'BHello' name=ID '!';
where BDsl would like to import ADsl via the commented-out import statement import "http://www.xtext.org/example/adsl/ADsl" as adsl for use in cross-referencing.
In the past, before uncommenting that import, I would have to add a resource reference referencedResource = "../org.xtext.example.adsl/model/generated/ADsl.genmodel" to GenerateBDsl.mwe2.
module org.xtext.example.bdsl.GenerateBDsl
import org.eclipse.xtext.xtext.generator.*
import org.eclipse.xtext.xtext.generator.model.project.*
var rootPath = ".."
Workflow {
component = XtextGenerator {
configuration = {
project = StandardProjectConfig {
baseName = "org.xtext.example.bdsl"
rootPath = rootPath
runtimeTest = {
enabled = true
}
eclipsePlugin = {
enabled = true
}
eclipsePluginTest = {
enabled = true
}
createEclipseMetaData = true
}
code = {
encoding = "UTF-8"
lineDelimiter = "\n"
fileHeader = "/*\n * generated by Xtext \${version}\n */"
}
}
language = StandardLanguage {
name = "org.xtext.example.bdsl.BDsl"
referencedResource = "../org.xtext.example.adsl/model/generated/ADsl.genmodel"
fileExtensions = "bdsl"
serializer = {
generateStub = false
}
validator = {
// composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
}
}
}
}
But when I generate the Xtext artifacts for BDsl, I now get the following error (the import still commented out).
434 [main] ERROR xt.generator.XtextGeneratorLanguage - Error loading 'ADsl.ecore'
The genmodel is certainly being found, since a completely different error is generated if the file cannot be found.
What is going on?
Am I making some stupid error?
Is this related to this bug? If so, is there a work around?
[... I am aware that the example contains no actual cross-references. I have purposely induced the error in the simplest possible manner. ...]
make sure you refer to the referenced genmodel in a way that it actually can be insolved. the ususal reference would look like platform:/resource/project/model/Some.genmodel so in your case referencedResource = "platform:/resource/org.xtext.example.adsl/model/generated/ADsl.genmodel"

No such property: getFlatConfig when trying to access configuration

So have set up a couple of values in the groovy.config file which I want for my application.
Set them as follows:
environments {
development {
grails.logging.jul.usebridge = true
reslist = ['1400x1200','1200x1024','1024x800','800x600']
resdef = '1024x800'
mapregs = ['World', 'Europe', 'Asia', 'South America','Central America', 'Pacific','Africa']
mapdef = 'World'
Then I try to access them in a controller
if ( params.mapreq == null) {
mapreq = grailsApplication.config.grails.mapdef
} else {
mapreq = params.mapreq
}
It seems to work (kind a) I get something back, but looks like an object pointer in the format
groovy.util.ConfigObject#3764a904
Tried changing it to getFlatConfig
if ( params.mapreq == null) {
mapreq = grailsApplication.getFlatConfig.grails.mapdef
} else {
mapreq = params.mapreq
}
At which point I get a "No such property: getFlatConfig when trying to access configuration" instead
So any suggestions?
Also, would the same solution work for getting the lists (like the mapregs one)?
grailsApplication.config.grails.mapdef should be grailsApplication.config.mapdef since mapdef is at the top level of the config (within that environment block). Since there's nothing stored under grails.mapdef, the value will be a new ConfigObject. That's why config.a.b.c.d=1 works - each time you access a new level that doesn't exist, Groovy automatically creates a new ConfigObject to hold the value being set, but if you're getting and not setting, you end up with just the empty instance.
The 2nd one doesn't work because getFlatConfig should be getFlatConfig() or flatConfig. But you can't use the ConfigObject-style dots with the flat config because it's flattened. If mapdef was actually under grails you'd access it as grailsApplication.flatConfig.'grails.mapdef' or grailsApplication.flatConfig['grails.mapdef']. But like the other one it's not, so you'd use grailsApplication.flatConfig.mapdef.

Using websockets/Server sent events with Angular dart

I was trying to use angular dart with websockets/server sent events and could not find any documentation/examples (there are some for angularJS but that seems very different for such things). A few things I tried also did not work.
Does anyone know how to do this?
Here is one version of what I tried and the error:
#NgController (
selector: "ACdistribution",
publishAs : "dstbn")
class ACDstbnController{
List <WtdPres> distbn;
void updateDstbn(List<WtdPres> newdstbn){
distbn = newdstbn;
}
final dstbnsrc = new EventSource("../dstbns")
..onMessage.listen((event){
List wps = JSON.decode(event.data);
List <WtdPres> newdistbn = wps.map((wp) => new WtdPres.fromJson(wp));
updateDstbn(newdistbn);
});
}
The error I got in pub build was:
web/provingground.dart:55:5:
'updateDstbn' is only available in instance methods.
updateDstbn(newdistbn);
^^^^^^^^^^^
There are limitations on what you can do on initializers for final fields.
Can you try to put this code inside the constructor
var dstbnsrc;
ACDstbnController() {
dstbnsrc = new EventSource("../dstbns")
..onMessage.listen((event){
List wps = JSON.decode(event.data);
List <WtdPres> newdistbn = wps.map((wp) => new WtdPres.fromJson(wp));
updateDstbn(newdistbn);
});
}

qrcode-0.1 with Grails 2.0.1

Look this is my problem: I have updated to Grails 2.0.1 and now I have to make a QR Code. I have installed the qrcode plugin 0.1 but it is not working. I'm usign the tag:
<qrcode:image text="${createPromoInstance.id}" />
but it seems it doesn't do anything. I debugged with google chrome and I realized that in the "Elements" tab, the tag is being changed for <call></call>
I was asking yesterday about this, and someone said me that the plugin has some bugs that doesn't work with Grails 2.0.1, and he gave me some advices about what can I do.
For example, I editted the QRController like this:
class QrcodeController{
QRCodeRenderer qrcodeRenderer = new QRCodeRenderer()
def index = {
qrcodeRenderer.renderPng(response, request.getHeader("REFERER"), 300i)
} //It doesn't have any change
def url = {
String uri = params.u
String size = getSize(params)
qrcodeRenderer.renderPng(response, uri, size.toInteger().intValue())
} //it doesn't have any change
protected String getSize(Map params){
String size = params.s
if(!size || size.matches(/\D\)) {size = "128"}
return size
} //I have added the "protected word"
def text = {
String content = params.t //it used to be params.text
String size = getSize(params)
qrcodeRenderer.renderPng(response, content, size.toInteger().intValue())
}
}
and he said if I make those changes it will work, but no, it doesn't! I'm trying to render the code in an empty gsp just to try it out like so:
<%page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title><title>
<head>
<body>
<div>
<qrcode:image text="${createPromoInstance.id} />
</div>
</body>
</html>
As I understand it should work, but it doesn't. Does anyone know what am I doing wrong? I have to do something else to get the rendered QR Code?
Thanks for the help!
Jonatan!
I found the answer... I will just leave it here if anyone need it.
to get this working you need to do some changes as I wrote before.
protected String getSize(Map params{ //it used to be String getSize(Map params)
String size = params.s
if (!size || size.matches(\/D/) {size = "128"}
return size
}
and
def text = {
String content = params.t //it used to be params.text
String size = getSize(params)
qrcodeRenderer.renderPng(response, content, size.toInteger().intValue())
}
but this is not everything, you have to change something else in the BuildConfig like so:
//find the plugin call
plugins{
//this is how my plugins call looks like, here you might see the calls of the plugins you have installed
runtime: ":hibernate:$grailsVersion"
runtime: ":jquery:1.7.1"
runtime: ":resources:1.1.6"
//and you gotta add this
compile: ":qrcode:0.1"
/*.
.
.*/
build: ":tomcat:$grailsVersion"
}
Then you must go to the plugin taglib "QRCodeTagLib" and replace this:
def image = {attrs->
def size = attrs.height?:attrs.width
String text = attrs.text
String src = createLink(controller:'qrcode',action:'text',params:[t:text,s:size])
//it used to be like this
/*def mkp = new groovy.xml.MarkupBuilder(out)
mkp{
img(alt:url, src:src)
}
*/
//and now it looks like this
out <<"<img alt=\"${text}\" src=\"${src}\"/>"
}
and that's it, your QR Code will be rendered!
Hope it will be useful for anyone!
Jonatan!!
PD: The credit is not mine for this code, somebody helped me on the grails facebook page! Lots of thanks for Ingo. :)
ah! Other thing... He added something in the bootstrap but it didn't work for me, in the bootstrap he put:
QRCode m = new QRCode()
m.save()
try it and let me know if that works for you! :)

Bootstrap doesn't save entities

I'm completely new to Grails and I'm doing the tutorial "Getting started with Grails Second Edition" by Scott Davis and Jason Rudolph.
When I'm setting up the BootStrap it seems like grails doesn't save the entities and I can't see them in my application.
I've already tried save(failOnError:true), but it compiles and saves without any errors.
Here is the code of BootStrap.groovy
package racetrack
import grails.util.GrailsUtil
class BootStrap {
def init = { servletContext ->
switch(GrailsUtil.environment){
case "development":
def jane = new Runner(
firstName:"Jane",
lastName:"Doe",
dateOfBirth:(new Date() - 365*30),
gender:"F",
address:"123 Main St",
city:"Goose",
state:"NC",
zipcode:"12345",
email:"jane#whereever.com"
)
jane.save()
if(jane.hasErrors()){
println jane.errors
}
def trot = new Race(
name:"Turkey Trot",
startDate:(new Date() + 90),
city:"Duck",
state:"NC",
distance:5.0,
cost:20.0,
maxRunners:350
)
trot.save()
if(trot.hasErrors()){
println trot.errors
}
def reg = new Registration(
paid:false,
runner:jane,
race:trot
)
reg.save()
if(reg.hasErrors()){
println reg.errors
}
break
case "production" : break
}
}
def destroy = { }
}
Thank you very much
are you in development mode?
Mybe you should add a log.debug or println statement to see if your code gets executed.
and have your tried a .save(flush:true, failOnError:true)? the flush might help
Use enums instead of Strings. Enums fit best in switch-case scenario and you can avoid mistakes. Maybe GrailsUtil.environment returns DEVELOPMENT not development? Instead of:
case "development"
use
case Environment.DEVELOPMENT
Using the grails.util.Environment enum doesn't work as is. Because the grails.util.GrailsUtil.environment returns a string, not an Environment.
You are more or less forced using strings.
Though old I thought it might help if I posted something I just found :)
if (GrailsUtil.developmentEnv){
// do dev stuff
}
else {
// do other stuff
}

Resources