Syntax - How to combine Binding with Localization? - binding

I need to combine localization with binding. I have the following xml:
<Label Text="{Binding TravelOrder.StartDate, StringFormat='Start Date: {0:d}'}" />
I would like to have the string format localized, i.e. instead of "Start Date" to have, for instance, for French, "Date de debut".
How can I put something like {x:Static res:AppRes.StartDate} inside my StringFormat value ?
I have tried to write something like
Text="{Binding TravelOrder.StartDate, StringFormat='{x:Static res:AppRes.StartDate}: {0:d}'}" />
but clearly that doesn't work.
Thank you.

Related

Twitter Character Counting

I want to implement a remaining character count feature for Twitter Twitt TextBox in Windows Phone 8 & Windows store 8.1.
Twitter limits Tweet length to 140 characters.If Textbox Text length exceeds 140 char,I'm not allowing user to twitt that text message.
If user enters any URLs in textBox,twitter api considers that Url length as 22 or 23.
How to implement a remaining character count feature with Urls?.If any method is available in Twiteer Api?
https://dev.twitter.com/overview/api/counting-characters
https://dev.twitter.com/overview/t.co
Thanks in advance.
This is a combination of simple, and surprisingly difficult.
So, I solved this with a behavior; I call it the SocialTextBoxBehavior.
You can download the source here: http://codepaste.net/j4ry9v
It easily supports a UI like this:
I certainly tested it to make sure it works. It seems to.
I added some nice properties you can bind back to your viewmodel:
MaxLength - you would likely set this to 140
MinLength - this is up to you, it defaults to 5
UrlLength - in your question you indicated 22 or 23 (it's a scalar value)
CurrentLength - current, adjusted text length; this is updated as the user types
CurrentRemaining - current, adjusted remaining; this is updated as the user types
IsValid - ensuring CurrentLength is between constraints; this is updated as the user types
The syntax to use the behavior is pretty straight-forward. I hope you think so.
Like this:
<StackPanel>
<TextBox Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<Interactivity:Interaction.Behaviors>
<Behaviors:SocialTextBoxBehavior x:Name="TwitterBehavior" />
</Interactivity:Interaction.Behaviors>
</TextBox>
<TextBlock Margin="0,10" DataContext="{Binding ElementName=TwitterBehavior}">
<Run Text="{Binding CurrentLength}" />
<Run Text="character(s) typed." />
<LineBreak />
<Run Text="{Binding RemainingLength}" />
<Run Text="character(s) remaining." />
<LineBreak />
<Run Text="{Binding MaxLength}" />
<Run Text="character(s) limit." />
<LineBreak />
<Run Text="Can submit? " />
<Run Text="{Binding IsValid}" />
</TextBlock>
<Button IsEnabled="{Binding IsValid, ElementName=TwitterBehavior}" />
</StackPanel>
Best of luck!

JSTL c:set and Struts s:set are undesirably formatting numbers

<c:set var="xmlDocumentId" value="${id}" scope="request" />
<s:set var="xmlDocumentId" value="%{id}" scope="request" />
are formatting id based on locale, setting xmlDocumentId to "12,345" while:
<c:out value="${id}" />
<s:property value="%{id}" />
are outputting "12345".
Any ideas how to break this behavior?
Because you are getting value with getText or <s:text> tag your long value gets formatted according to locale. To prevent this from happening convert your long to string.
With <s:set> tag you can call toString() method directly in value attribute.
<s:set var="xmlDocumentId" value="id.toString()" scope="request" />
For the concrete formatting algorithm take a look at java.text.MessageFormat class and its subformat method.
Once you know how to format numbers in Java, in Struts2 <s:property/> tag you can use getText() to format your number in the desired way, for example :
<s:property value="getText('{0,number,#,##0}',{id})"/>
Try this
<s:text name="id" > <s:param name="value" value="id"/> </s:text>

How to simplify assigning the same converter to multiple bindings (windows store app)?

<TextBlock Visibility="{Binding IsTrue1, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<TextBlock Visibility="{Binding IsTrue2, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<TextBlock Visibility="{Binding IsTrue3, Converter={StaticResource BooleanToVisibilityConverter}}"/>
The "Converter" Property is on Binding, not on TextBlock, so I can't use a style on TextBlock.
Each binding is different, so I can't create a single Binding resource.
So, how to avoid setting the same converter 3 times?
EDIT: I'll try to explain a bit more. What I'm looking for is a way to give the binding object a default converter, so that I don't have to set the same converter over and over again when I create many bindings with the same converter.
So if I can write sth like:
<Grid DefaultBindingConverter="{StaticResource BooleanToVisibilityConverter}">
<TextBlock Visibility="{Binding IsTrue1}"/>
<TextBlock Visibility="{Binding IsTrue2}"/>
<TextBlock Visibility="{Binding IsTrue3}"/>
...
Clearly this is not correct, just to illustrate my idea.
Hope this time I explained it clear enough.
Let's touch about two issues in your question.
1. Single Resource
It seems that you are misunderstood about the StaticResource definition in Converter.
The BooleanToVisibility that you've coded, for example, is worked not three-times-copied object. It only declared once and used three times.
Let's take another example, if you code as below
int i;
i=1;
i=2;
i=3;
You declared i once, and used it three times. Likewise, StaticResource that you used are works as similar. You may declare x:Key="BooleanToVisibility" in <UserControl.Resources> or <Application.Resources> tag, that's it.
2. Style usage
If you want to set Style in TextBlock, you may use below approach.
<TextBlock Visibility="{Binding Number1, Converter={StaticResource BooleanToVisibilityConverter}}">
<TextBlock.Style>
<Style>
<!-- Define your Styles here -->
</Style>
</TextBlock.Style>
<TextBlock>
As you can see above, You can expand Style XAML attribute into inner tag.
==EDIT==
I now understand your intention. You probably want to apply Converter in a hierarchical inheritance manner like a DataContext works.
Sadly, as far as I know, that is NOT possible under XAML. Because each Binding is just a property so that it has to be applied one-by-one.
Possible workaround is to use code-behind to enumerate elements and apply them.
for(int i=0;i<3;i++){
var textbox = (TextBox)this.FindName("TextBox" + i);
var binding = new Binding("IsTrue" + i);
binding.Converter = new YourDefaultConverter();
textbox.SetBinding(TextBox.TextProperty, binding);
}
hope this helps to reduce your chores.

translate a HTML select element in Grails

thought this would be easier.... imagine a <g:select /> like this:
<g:select name="type.id" from="${Type.list()}"
value="${domainInstance?.type?.id}" />
with two domain classes like this (please forgive me if these artificial classes are not error free)
class Domain {
Type type
}
class Type {
String name
}
I would now like to translate the entries of the select element. The following code first looked good:
<g:select name="type.id" from="${Type.list()}"
valueMessagePrefix="type.name"
value="${domainInstance?.type?.id}" />
with entries in the messagebundle like this:
type.name.type1 = red
type.name.type2 = green
Problem: not only the text was translated, but the option keys, too!
So I tried to add a optionKey='id':
<g:select name="type.id" from="${Type.list()}"
valueMessagePrefix="type.name"
value="${domainInstance?.type?.id}"
optionKey='id' />
This switched the keys to the id - great, but the text switched to the id, too :-(
Any idea how to solve this?
thanx to grails beeing open source, I just checked the code: http://grails.org/doc/latest/ref/Tags/select.html#select
It seems that valueMessagePrefix is ignored as soon as you use optionKey or optionValue. But optionValue can take a closure:
<g:select name="type.id" from="${Type.list()}"
value="${domainInstance?.type?.id}"
optionKey="id"
optionValue="${ {name->g.message(code:'type.name'+name) } }"/>
at least, this works.
Can't you just add an optionValue?
<g:select name="type.id" from="${Type.list()}"
valueMessagePrefix="type.name"
value="${domainInstance?.type?.id}"
optionKey='id'
optionValue='name'/>
Sorry I haven't had a chance to test this exact code, but have done similar things like this with no problems.

Grails: How to include an html link inside a <g:message> default attribute?

I am starting with Grails and want to have one page with multilanguage content.
I started using the tag which works fine.
But here is what I want to do:
I want to include the default text of the default language right in the text, to avoid switching back and forth in between files.
<g:message code="homepage.feature.headline1" default="This is an english text" />
The above works.
But now I a have a message which should include a link like this:
<g:message code="homepage.feature.headline1" default="This is an english text with <a href='somefile.html'>a link</a>" />
This gives me an exception:
org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: Grails tags were not closed! [[<g:message>]] in GSP
How can I achieve that I can include a link there? I have tried escaping the <> brackets but still no luck.
I really would like to avoid splitting up this one sentence into multiple smaller sentences.
Thanks
Christoph
Closures can be used for nested i18n messages. I find it useful for when the link generation needs additional logic.
<g:message code="homepage.feature.headline1" default="This is an english text with a {0}" encodeAs="raw" args="[link(controller: 'someController', action: 'someAction') { message(code:'homepage.feature.headline1.link')}]"/>
You have two possible ways:
<g:message code="homepage.feature.headline1" default="This is an english text with ${'<a href=\'somefile.html\'>a link</a>'}" />
or
<% def link = "<a href='somefile.html'>a link</a>"%>
<g:message code="homepage.feature.headline1" default="This is an english text with $link" />
I usually create 2 messages: the original one and the key other to be replaced. This makes it optional to create the link or not. Considering both keys are provided (leaving the verification out of this snippet):
packagesUpdate.error.server.unreachable=The packages repository server "{0}" is unreachable. This usually happens behind a network proxy server.
packagesUpdate.error.server.proxyReplace=network proxy server
Replacing the string in the controller, e.g., on ref msgWithLink:
def msg = message(code: 'packagesUpdate.error.server.unreachable')
def proxyReplace = message(code:'packagesUpdate.error.server.proxyReplace')
def msgWithLink = msg.replace(proxyReplace,
"<a href='/csvn/packagesUpdate/available'>${proxyReplace}</a>")
Replacing in the g:message tag:
< g:set var="msg" value="${message(code:'packagesUpdate.error.server.unreachable')}" />
< g:set var="proxyReplace" value="${message(code:'packagesUpdate.error.server.proxyReplace')}" />
< g:set var="link" value="${proxyReplace}" />
< g:set var="msgWithLink" value="${msg.replace(proxyReplace, link)}" />

Resources