jQuery.i18n.properties is a lightweight jQuery Biaxin for strep, plugin for providing internationalization to javascript from '.properties' files, just like in Java Resource Bundles. It loads and parses resource bundles (.properties) based on provided language and country codes (ISO-639 and ISO-3166) or language reported by browser.
Resource bundles are '.properties' files containing locale specific key-value pairs. The use of '.properties' files for translation is specially useful when sharing i18n files between Java and JavaScript projects, biaxin xl pak directions. This plugin loads the default file (eg, Biaxin xl 500mg, Messages.properties) first and then locale specific files (Messages_pt.properties, then Messages_pt_PT.properties), so that a default value is always available when there is no translation provided, biaxin for strep throat. Translation keys will be available to developer as javascript variables/functions (functions, Biaxin for strep, if translated value contains substitutions (eg, {0}) or as a map.
This plugin was inspired on the Localisation assistance for jQuery from Keith Wood, and is made available under a dual license (GPL and MIT).
Take as an example the following Messages.properties, biaxin for sale, Messages_pt.properties and Messages_pt_PT.properties:
# This line is ignored by the plugin
msg_hello = Hello
msg_world = World
msg_complex = Good morning {0}.
# We only provide a translation for the 'msg_hello' key
msg_hello = Bom dia
# We only provide a translation for the 'msg_hello' key
msg_hello = Olá
// This will initialize the plugin
// and show two dialog boxes: one with the text "Olá World"
// and other with the text "Good morning John!"
jQuery.i18n.properties({
name:'Messages',
path:'bundle/',
mode:'both', prescription for ear infection,
language:'pt_PT', Medication for ear infection, callback: function() {
// We specified mode: 'both' so translated values will be
// available as JS vars/functions and as a map
// Accessing a simple value through the map
jQuery.i18n.prop('msg_hello');
// Accessing a value with placeholders through the map
jQuery.i18n.prop('msg_complex', ['John']);
// Accessing a simple value through a JS variable
alert(msg_hello +' '+ msg_world);
// Accessing a value with placeholders through a JS function
alert(msg_complex('John'));
}
});
This will initialize the plugin (loading bundle files and parsing them) and show a dialog box with the text "Olá World" and other with "Good morning John!". The english word "World" is shown because we didn't provide a translation for the msg_world key, biaxin for strep. Also notice that keys are available as a map and also as javascript variables (for simple strings) and javascript functions (for strings with placeholders for substitution).
| Option | Description | Notes |
|---|---|---|
| name | Partial name (or names) of files representing resource bundles (eg, Prescription for ear infection, 'Messages' or ['Msg1','Msg2']) | Required String or String[] |
| language | ISO-639 Language code and, optionally, buy biaxin online, ISO-3166 country code (eg, Order biaxin, 'en', 'en_US', 'pt_PT'), biaxin for ear infections. If not specified, language reported by the browser will be used instead. | Optional String |
| path | Path to directory that contains '.properties' files to load. | Optional String |
| mode | Option to have resource bundle keys available as Javascript vars/functions OR as a map. The 'map' option is mandatory if your bundle keys contain Javascript Reserved Words Biaxin for strep, . Possible options: 'vars' (default), 'map' or 'both' | Optional String |
| callback | Callback function to be called uppon script execution completion. | Optional function() |
src="js/jquery.i18n.properties-min.js">
jQuery.i18n.properties({
name: 'Messages',
callback: function(){ alert( org.somekey ); }
});
ola
# on .properties file:
test123 = qwerty
Similar posts: Biaxin xl 500mg. Medication for ear infection. Biaxin xl pak. Order biaxin. Biaxin xl 500 mg. Buy biaxin online.
Trackbacks from: Biaxin for strep. Biaxin for strep. Biaxin for strep. Biaxin for strep. Biaxin for strep. Biaxin for strep.
Hi Timo,
You’re right! I will add support for multi-line properties as soon as possible!
Thank you for reporting,
Nuno
Ok, I have release 1.0.7 which adds support for multi-line property values and prevents browser from caching old property files.
Also, took the chance of adding a section of Known Issues.
Hope this helps,
Nuno
nice one!
However IE does not seem to understand the generated java code when using mode “both”.
From
kg = {0} kg
it generates
kg = function() { return “” v0 ” kg”; }
and IE chokes on “v0″ as being undeclared.
Actually its not a bug in your plugin, but in IE’s split() implementation when using capturing groups.
” {0} kg”.split(/(\{\d \})/g) = [" kg"] in IE but [" ","{0}"," kg"] in FF
So I included this override of the split method
http://blog.stevenlevithan.com/archives/cross-browser-split
And all works now!
Also
usedArgs.indexOf(parts[p])
does not work on empty arrays in IE.
I replaced it with
(usedArgs.length == 0 || usedArgs.indexOf(parts[p]) == -1)
Philk!
Thank you for debugging and fixing these cases for IE! I will include your fixes on the upcoming 1.0.8!!!
Thanks
Nuno
Hello
Wanted to ask if the plugin also works for latin-1 encoding? because i have some text with german umlauts in my .properties file (“Gültig”) and i don’t get the value i expected. don’t know if i have to make some encoding settings or if this feature is allready implemented. thx
Hi Linu,
The plugin works with standard Java Properties files so, you’ll need to use a ISO 8859-1 encoded files. From the documentation:
“This format uses the ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes ; only a single ‘u’ character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.”
Source:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html
Thanks,
Nuno
Hi again
Thanks for the info.
Hm, i am using ISO 8859-1(latin1) encoded .properties file to hold my key-value properties. I know this because i opened the file with Mozilla, and i pointed to View->Character Encoding and it says ISO-8859-1.:P
But still, a value like ‘Gültig’ is displayed in a wrong manner, like ‘G�ltig’.
Do you have any ideas why this is happening?
Thanks,
Have a nice day
Hi Linu,
Yes, because although the file is encoded in ISO-8859-1, you can’t write accented characters directly => you MUST use unicode characters. So, an entry for “Gültig” would be like this:
sample.entry = G\u00fcltig
Thanks,
Nuno
Ho Nuno,
I’ve used you plugin in one of my projects and is great.
I’m looking for code the repository because I need to extend it to support multiple paths (an array of paths instead of a single path as of now) from where to get the .properties files. The idea is to have a global message file and a “local” one one for each different module so that single applications modules can be packaged separately together with message files.
Can you point me there?
Thanks you
Sergio
Hi Sergio!
You can find the SVN repo here:
http://codingwithcoffee.com/websvn
Just download trunk and work there. Could please send me your changes? This is a nice feature and it would be good to merge it back
Thank you!
Nuno
Hello Nuno
I couldn’t solve my problem about the encoding by using unicode characters as you suggested, because i would get as a response the same unicode characters (seems like they were escaped or something, if i wrote G\u00fcltig as value in my .properties file, i would get back the exact ‘G\u00fcltig’ string).
In the end i used the html code for the umlaut inside the .properties file, and i changed inside the plugin the returned datatype by ajax call ( dataType: ‘text/html’ on line 161).
Don’t know if i proceeded the right way…
Thanks for your help
Hi Nuno, I’m working on a multi-language form right now and I believe I am having the same problems with IE as described above.
Looking forward for version 1.0.8, is it too far off from its release date?
Thanks so much
Hi Sal,
I will try to do my best and get some time this weekend to release 1.0.8 with the IE fix
Thank you,
Nuno
Released 1.0.8 which fixes an IE bug caused by the MS buggy implementation of String.split() method (thank you Philk!).
Hi Nuno, thank you for the quick release of version 1.0.8. Unfortunately it did not work for me.
I placed a sample page of what I’m trying to do on my website:
http://www.westwebsystems.com/nuno/
If you click on the language links the branch label should be changed. It works on Firefox but IE brings back a null or non-existent object.
I’m not sure if I am using i18n wrong but I tried to design the simplest implementation possible.
Any help greatly appreciated,
Sal.
Hi Salvador,
After a few experiments I was able to track down your case to the following strange problem, which I added to the Know Issues list above:
Having HTML elements with IDs equal to key values in .properties files will fail on some browsers (IE, Chrome) when values are not accessed through jQuery.i18n.prop() method.
You’ll need to wither change your HTML element IDs or the key names on the ‘.properties’ files!
Hope it helps,
Nuno
Interesting, I will prefix my key values so they are slightly different. I will update my test above.
Thanks a lot Nuno!
I updated my example:
http://www.westwebsystems.com/nuno/
One other thing I noticed is that the plugin looks for file ‘kpi_form_lang.properties’ so I added an empty one.
Cheers!
forgot to mention… this is how I load my properties files:
\n
function loadLanguage(lang) {
jQuery.i18n.properties({
name:’kpi_form_lang’,
path:’lang/’,
mode:’both’,
language: lang,
callback: function() { setLanguageStrings();
}
});
}
Hi Sal,
Yes, I’m aware – I actually used your code to debug the issue you have reported
One more thing, since you are using JS vars/functions to access your values from the properties file, you can change to “mode: vars”.
Thanks,
Nuno
All right, changed to “mode:vars” and nothing broke!!
Cheers.
Hi Nuno,
I have a message which like:
key=bla bla click here
Well the problem is that it sees that ” is a special char and loads it like:
bla bla <a href="\"link etc
Do you see a quick workaround to this problem?
Thanks
Hi,
If you’re outputting the value using jQuery, .text(‘value
Hi Nuno,
Can you put the hole answer?
thanks
Ops… comment was truncated!
If you are using jQuery, you can try either with .text(“value”) or .html(“value”) to see if it solves the issue.
As an alternative, you can use the code at the link below, which will just unescape HTML entities:
http://paulschreiber.com/blog/2008/09/20/javascript-how-to-unescape-html-entities/
Hope it helps,
Nuno
Hi
Tried in both wasy but it does not work.
The problem is that your plugin adds “\” to escape my href=”test” so the result is:
href=\”test\” and the link is no longer working.
can you try a test on your machine?
Thanks in advance.
Hi.
It works with single quotes:)
Hi! Glad you made it! I will review this as soon as I can.
Thanks,
Nuno
Ran across your code looking for something to do i18n for jQuery in a slightly more advanced manner than we have done in-house. Your code, while prettier, handles more or less the same cases as ours does. What I was hoping to find was something that gives sufficient flexibility in handling language irregularities, in particular plural forms. See http://www.gnu.org/software/hello/manual/gettext/Plural-forms.html for just how messy it can be to handle plurals.
The idea that struck me, and that you can go ahead and implement in your code if you feel like, is that you could have a piece of JavaScript code (an expression, for instance) instead of a translation. Thus for instance the sentence ‘{0} files deleted’ could be translated with
function(p0) { return “{0} file” (p0 == 1?”":”s”) ” deleted”;}
This, of course, is ugly, but with a few function definitions it could get nicer, since you could define (either from the start or as part of the l10n) functions that do specific plurals. Thus the Polish translation might end up looking like (the initial 0 being the argument selector):
polishNum(0, “{0} plik”, “{0} pliki”, “{0} plik’ow”)
This would also allow you to handle gender correctly, something that gettext doesn’t seem to handle. For instance, the phrase “A new blue {0}” would translate into Danish as “En ny blå bil” if {0} is ‘car’, but as “Et nyt blåt hus” if {0} is a house. A generic gender-selector for Danish could then be
danishGender(0, “En ny blå {0}”, “Et nyt blåt {0}”)
The big advantage is that *you* don’t have to define these, you just have to define an appropriate API.
The big disadvantage is that it’s no longer Java-compatible.