I'm developing web script for Alfresco 5.0d CE and encountered an issue: I cannot interpolate datetime value of type org.mozilla.javascript.NativeDate in my FreeMarker template directly (or with embedded FreeMarker methods ?date, ?time or ?datetime).
${var.startDate}
interpolates into
org.mozilla.javascript.NativeDate#<some_hash>
Please, advise me, how can I do that?
Can you introduce your own FreeMarker utility methods in Alfresco? Because then there you can call the static org.mozilla.javascript.Context.toType(valueFromRhyno, Date.class) method to convert the Rhyno JavaScript date to a Java java.util.Date. So let's say you implement that in Java, then expose that utility to FreeMarker. I don't know how to do that in Alfresco, but FreeMarker itself supports that. Then you can do something like ${myJsUtils.toJava(var.startDate)}.
Actually, FreeMarker's ObjectWrapper facility is designed to deal with these kind of mismatches. If you can use a custom ObjectWrapper, then it could just work magically, as then FreeMarker will know how to convert the Rhyno objects automatically. But I guess changing such a core setting under Alfrescho can be tricky. Too bad they themselves didn't do that.
Related
I'm using kotlin2js to generate JS library from Kotlin code. I'm then using this library in Javascript (not Kotlin). The code has some Kotlin objects and some normal classes. I can access normal classes from Javascript, but I can't access the objects in any way.
The documentation is pretty sparse, only relevant line might be this:
Kotlin preserves lazy object initialization in JavaScript.
I'm not sure what that means.
I suppose you should specify moduleName as well when accessing from javascript.
The problem was that Kotlin changed name of the method to something like this: calculate_ywek2$(). And it's very hard to figure out, because Kotlin doesn't generate Typescript definitions, so autocomplete doesn't work. The name can be changed with #JsName annotation.
In my HTML page I'm loading my translation with Thymeleaf like that:
<button type="button" ng-click="$ctrl.resetData()" th:text="#{general.reset}">Reset</button>
Is it possible to translate text wich is hard coded in my JavaScript code?
'Loading Data...' should be translated with th:text="#{general.loadingData}" <- but this is the way I would translate it im my HTML page, this doesn't work for JavaScript Code. All the translations are in a messeges_en.properties file.
How can I translate the text in JavaScript?
function(request: any) {
asLoading.show('Loading Data...');
return request || $q.when(request);
}
You should use javascript Intl API.
FormatJs is a modular collection of JavaScript libraries for internationalization that are focused on formatting numbers, dates, and strings for displaying to people.
It has support for ICU message format (International Components for Unicode) which is also well supported in java as well.
So you can expose your translations in ICU message format and consume them from both java and javascript.
In general, it is better to treat javascript and java code as separate applications as opposed to treating javascript as something that is tacked on top of a java template language like thymeleaf.
I'm thinking about using TypeScript to create an online app, and this will require saving of data online.
What are my options in regards to that?
Is there anything specific to using TypeScript which makes that easier or harder?
Ideally I would use a service like Parse.com to save data, can Typescript be connected to Parse or would I have to rely upon plain JS?
TypeScript runs wherever javascript runs. So
Your options are the same as javascript.
Typescript compiles down to javascript. And it is designed to be a superset of javascript so your javascript will be valid typescript as long as you have variables declared and sometimes types mentioned.
Optional static typing + easier syntax is what makes developing in TypeScript easier.
Static typing makes refactoring and intellisense more reliable. Having an easier syntax for classes / modules means you are more likely to structure your code better.
Yes you can use parse.com with typescript
The recommended way to do that is to create a declaration file describing your javascript code. In the beginning it can be as simple as:
declare var parse:any;
I wrote some guidance on the matter here : http://basarat.github.io/TypeScriptDeepDive/#/declarations
There is a huge resource of declaration files you can find at https://github.com/borisyankov/DefinitelyTyped . In particular check out FireBase : https://www.firebase.com/ and its declaration file : https://github.com/borisyankov/DefinitelyTyped/tree/master/firebase However there isn't one on parse.com yet which is why I mentioned the way to write your own.
Additionally you don't need a declaration file if you do not want any impressive static checking of the typescript code that interacts with the parse.com's api.
i'm very new to Handlebar js. In every article about the HandleBar js there is a word called (mustache js). So,
1. What is the relation between Handllebar js and Mustache js?
2. Is it necessary to read mustache js before handlebar js?
3. What s advantage of HandleBar js over Mustache js?
Thanks.
Handlebars.js is a superset of Mustache.js. It offers all the same ability to insert JSON into templates plus additional abilities to do some basic if/then, etc. In general, I find it much easier to do templating with Handlebars.
Mustache is very much about not having any logic in your template at all. Every piece that gets inserted into it should be pre-chewed so there's no need for any logic. I find that to not at all be realistic.
Just realized that I didn't answer one of your questions. No, it is not necessary to read about Mustache before using Handlebars. The Handlebars documentation is sufficient on its own.
P.S. One of our favorite Handlebars features is "helpers" which allow you to create new keywords in the language for specific things (for example, we use one to format numbers and sometimes replace them with "N/A" for zero values).
P.P.S. A great way to try out Handlebars and see some templates in action (and also to play with your own templates and JSON) is http://www.tryhandlebarsjs.com/
According to this website (which also provides a benchmark between the two)
handlebars.js is a compiled mustache implementation with some additional
features
My translation mechanism works serverside with the jinja2 template engine, webapp2's i18n function with the magic _ function and now I need it for Javascript to localize just a few strings but I couldn't find a good implementation. Is there one?
I'd like a solution for localizing my web app. The javascript strings are just a few but I need a translation mechanism and the dictionary is .po and .mo files. Ideally I'd like javascript to take the same dictionaries as python does (the .po files)
I don't need extractions, what I need is the _ function plus some way of determining user language and loading the translations, not just for a single language but for all my languages. I looked at some solutions but they only handla one translation at a time and I need to handle many.
For instance, http://www.zomeoff.com/jsin/jsin.1.2.unit.test.html does a successful job but this is only one localization. I need to harmonize the translations so that the same language is used by both the python jinja2 templates and javascript.
Do you have s suggestion or can comment my situation?
Thank yo
Javascript Gettext
You could probably use polib and json to generate the JSON on the fly though.