We need to be able to localize strings in javascript - thinking for things like the app_offline.htm file etc.
jquery globalize is hectic and seems like total overkill. Is there a simple jquery plugin or anything really that will allow us to localize js strings?
At the risk of over simplifying:
var globals = {
en-US: {
color:'color',
cell:'cell phone'
},
en-GB: {
color: 'colour',
cell: 'mobile phone'
}
};
To use:
text = globals[lang].color;
where lang = 'en-US' etc
You can either generate that structure on the server and use resource files etc there, or just keep this object literal in a global.js or similar.
The Globalize.js library, previously known as jquery-global or jQuery Globalize, is relatively small, but if you only need string localization (and not date and number localization as well), then it does not offer much more than a general setup: locale (culture) object containing the property messages. It is initialized to an empty object, and you are supposed to add properties to it, corresponding to your strings to be localized. And it has the simple method Globalize.localize() that selects a localized string for a key
To implement simple string localization, you do not necessarily need any library or plugin. You could just code some simple approach like that of Globalize.js; the general code is fairly simple, much less work than defining the actual localizations for each string. On the other hand, if you have localization needs, you might just as well use Globalize.js, preparing for other kinds of localization in the future.
Related
Going through https://www.npmjs.com/package/i18n and https://github.com/fnando/i18n-js it`s a pretty common approach to have a locales file with a translate function like i18n.t('string'), and multiple translation files, using the received string to find the proper translations
ie (from i18n-js):
I18n.t("some.scoped.translation");
// or translate with explicit setting of locale
I18n.t("some.scoped.translation", { locale: "fr" });
Well, why instead of using those string paths, don't we just access the translate JSON directly?
it would still be possible to use the i18N lib to get the user language and even set it, but instead of using .t() method, we could just redirect to the proper translation object.
Wouldn't it help avoid typos since you will use the object itself to verify the path?
it seems like a so well diffused practice to use the locales string, I feel like I'm missing something about my approach, but couldn`t get to what it might be, I considered that it would be a heavier duty to go through the whole strings data, but i18n.t() would still have to do it plus parse the string into object path
I'm developing a web application with Go on the server, and the router will use PushState, so the server will also have to be able to render my templates. That means that I'll need a templating engine that works with Go and Javascript. The only one I've come across so far is Mustache, but it doesn't seem to be able to handle lowercase properties of structs, and there also doesn't seem to be a possibility to provide custom names like JSON:
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
So, is there a templating engine that is available in both Go and JavaScript, and that can handle lowercase struct properties?
As the comments above state, you can't expect any 3rd party library to be able to read lowercase properties on your struct, but it appears you are trying to use tags to represent alternative representations of your struct (as you can with the encoding/json library).
What you can do is use something like github.com/fatih/structs to convert your structs to maps and then range through to lowercase all of your keys (copying the values and removing the uppercase versions) and pass it into mustache.Render() as your context. If you want to use struct tags like the encoding/json library does, you'd have to use the reflect package and write a struct-to-map function that takes into account the tags on the struct (basic example given in the documentation here). There are some SO answers on how to write a struct-to-map function using reflection, which you can improve upon to add struct tag handling as you need.
To answer your question, I don't think this is something a current templating library does that also works with javascript, but it shouldn't be too hard to get working with mustache given the idea above.
Is there any library already available for javascript data format / transformation?
Example: Date, Time, Numbers, etc., can be converted to text with predefined formats.
These formats are chiefly locale based. But some how the mechanism to convert value (data, date, time..) should be there, that accepts custom format too to translate into desired formatted text.
On the contrary, the same library should be capable of parsing the text to value, with the source format knowledge.
Some thing similar to Spring's Property Editors or Converter.
Quick Searches; http://numeraljs.com/ http://www.datejs.com/
I should have mentioned the fact that, I am using DOJO as our JS Widget Library and with it comes methods and classes to address formatting / parsing, bundled.
Now that we have DOJO in place, I will be putting the code in the abstract layer (wrapper) over the library to keep the solution Library-Agnostic.
Find the code below - for brevity only the snippet is mentioned, could have done JSFiddle (may be in a day or two).
// get the DOJO handle, either from global "dojo" variable or via AMD
// var dojo = require('dojo');
............
// for date
dojo.date.locale.format(new Date(), {selector: "date",datePattern:"dd/mm/yyyy"});
// output: 31/06/2013
// for number
dojo.number.format(9999999.99,{type: "decimal",pattern:"#,##,####.##"});
// output: 9,99,9999.99
............
NOTE: An important feature it addresses is that it understands locale and can do the formatting automatically w.r.t the locale configured. DOJO library staunchly follows Java style of data formatting. If you are a Java Geek, you would find the library and format semantics easy to follow.
Find the DOJO link in this respect ://dojotoolkit.org/reference-guide/1.7/quickstart/numbersDates.html (could not post the complete link as I do not have enough reputations, please prefix "http" to the link)
Still, I would like to welcome better solution if any, for the given context.
As part of libraries used, we are using jQuery, Underscore, Backbone and DOJO primarily , all across for specific purposes in the application.
I use Moment.js for all my JS date formatting and manipulations. http://momentjs.com/
I have to make a invite your facebook friends module which fetches the names, photos of your friends and allows you to message 'em. I need this to look like a integral part of my website so I have to style it. I fetch the json with friends' ids, names etc. and want to put those values in certain html tags and attributes. How do I apporach this? I can make it in jQuery but want to avoid jQuery spaghetti code with ragu of strings and vars. What lib/template engine do you recommend me? Ease of use and weight are the most important things. The website has jQuery already included.
I can make it in jQuery but want to avoid jQuery spaghetti code with ragu of strings and vars. What lib/template engine do you recommend me?
I’d suggest to use no big additional lib or template engine – I’d just keep using jQuery, and embed one of the sprintf for jQuery implementations that are floating around the net.
So you can define your “HTML template” for your output in one location as one string, and than replace placeholders in that string with variable values while your looping over the data in jQuery.
If you don’t like any of the sprintf-Jquery-plugins out there, here is another very simple and short function that implements just the basic string placeholder %s (but more than that you most likely won’t need anyway): http://www.nczonline.net/blog/2011/10/11/simple-maintainable-templating-with-javascript/.
(And if you have to insert values in multiple places of your template string, than have a look at my comment on the bottom of that page, where if have proposed a simple adjustment to Nicolas’ function, that implements the “argument swapping” feature of PHP’s sprintf, so that you have to pass values to the function only once, but can use them in multiple places in your template string.)
The easiest way to do this is to use the Requests dialog. The first thing you need to do is create an app. Once you have this you should be able to use the JavaScript example on the request dialog page.
This is something of an embarrassing question to ask, seeing as it is mainly about my lack of Javascript knowledge. Seems like it might be "so obvious" that nobody specifies it anymore. Gotta start somewhere! I'm a proficient coder in other languages, I'm just unfamiliar with JS.
On the ReCaptcha Client API docs page, there's an example of the proper format for the customization options dictionary.
I'm writing a 3rd party app which generates that dictionary based on user prefs. I don't know of / couldn't find a dictionary type in Javascript. My best guess is that the dictionary is JSON. I want to make sure that my output is valid and conformant to what they expect, particularly with regards to quotes (at all? single? double? etc.)
Is it JSON? Is it another type of dictionary? Can anybody point me at some kind of specs for how a valid instance of a JS dictionary looks? Googling for "Javascript Dictionary" has proved ineffective owing to the generic quality of the search terms.
Thanks!
It's a javascript object literal, which is almost the same as JSON (it's a superset).
Any valid JSON will work here, but as you can see from their example, Javascript has a few other options:
<script>
var RecaptchaOptions = {
theme : 'white',
tabindex : 2
};
</script>
The part between the braces is valid Javascript but not valid JSON because theme, tabindex and white should all be double-quoted. Below is valid JSON embedded in Javascript, which works perfectly:
<script>
var RecaptchaOptions = {
"theme" : "white",
"tabindex" : 2
};
</script>
No, it’s not JSON, it’s plain JavaScript. More exact: It is a simple object declared using the object initialiser syntax { … }.
That syntax is similar to JSON as JSON is a subset of JavaScript. But JSON requires the property names to be a quoted string, JavaScript not.