Jump to content


Important Announcement!

Please read this post about our new Privacy Policy


Photo
- - - - -

How to localize strings in JS files?


  • Please log in to reply
8 replies to this topic

#1 ericjung

ericjung

    Advanced Member

  • Members
  • 235 posts

Posted 08 May 2006 - 02:54 AM

Hi,

I've read all the tutorials about how to localize strings in JS files, but I cannot get it to work. Here's what I do:

In the overlay:

CODE
  <stringbundleset id="stringbundleset">
    <stringbundle id="foxyproxy-strings" src="chrome://foxyproxy/locale/foxyproxy.properties"/>
  </stringbundleset>


In foxyproxy.properties:

CODE

invalid.url=The URL specified for automatic proxy configuration is not a valid URL.


In the JS:

CODE
var strbundle = document.getElementById("foxyproxy-strings");
foxyproxy.alert(this, strbundle.getString("invalid.url"));


The error I get is:
CODE

strbundle has no properties


What am I doing wrong? Help!

Thank you in advance,
grimholtz

#2 dafi

dafi

    WTS Developer

  • WTS Developer
  • PipPipPip
  • 498 posts

Posted 08 May 2006 - 08:07 AM

Hi grimholtz nice to meet you here on BZ,

Your code seems correct and I have had some problem with <stringbundleset/> tag.

I prefer to retrive strings from properties using nsIStringBundleService service
CODE

Common.strings = Components.classes["@mozilla.org/intl/stringbundle;1"]
    .getService(Components.interfaces.nsIStringBundleService)
    .createBundle("chrome://foxyproxy/locale/foxyproxy.properties");

Common.getLocalizedMessage = function(msg) {
    return Common.strings.GetStringFromName(msg);
}

Common.getFormattedMessage = function(msg, ar) {
    return Common.strings.formatStringFromName(msg, ar, ar.length);
}

You can retrieve the value using

CODE

        var nativeLabel = Common.getLocalizedMessage("invalid.url");


For better use take a look at ViewSourceWith code
Davide Ficano
Author of the ViewSourceWith extension
http://dafizilla.sourceforge.net/viewsourcewith

#3 ericjung

ericjung

    Advanced Member

  • Members
  • 235 posts

Posted 08 May 2006 - 02:18 PM

Thanks Davide, I'll give that a try.

#4 Diegoratt

Diegoratt

    Newbie

  • Members
  • 8 posts

Posted 11 February 2008 - 09:14 AM

Hi. I had the exactly the same problems grimholtz had and resolved them this way using dafi code:

CODE
var common = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService).createBundle("chrome://myext/locale/myext.properties");

function getLocalizedMessage(msg) {
    return common.GetStringFromName(msg);
}

alert(getLocalizedMessage('proprietyname));


Thanks dafi

Someone must put this solution in the MDC page.

#5 markh

markh

    BZ Jungle Guide

  • Admin
  • 4070 posts

Posted 11 February 2008 - 10:01 AM

QUOTE(Diegoratt @ Feb 11 2008, 09:14) <{POST_SNAPBACK}>
Thanks dafi

Someone must put this solution in the MDC page.

dafi has developed a wonderful extension to automate the yawn-bringing task of localizing: Externalize strings.

wink.gif
While the bankers all get their bonuses I’ll just get along with what I’ve got


#6 DaveG

DaveG

    Techie Tricks and Tips provider

  • Super Mod
  • 447 posts

Posted 11 February 2008 - 07:28 PM

Your issue may be that the "document" you're searching in via the script is not the document the string bundle is in via the XUL. You might want to explore the tree with DOM inspector and see where it's showing things, as it's just not finding it.

On a side note, I too got a similar error a while back, though my problem was the ID of the string bundle set. It must be as you have it for browser overlays, or it will not work due to some obscure bug. If you're not in an overlay with this, you should probably use a different ID. (in fact, when I use a stringbundle in a separate window, I give the set no ID) Not sure if this'll help; sadly this particular system is a tad buggy.

#7 ericjung

ericjung

    Advanced Member

  • Members
  • 235 posts

Posted 11 February 2008 - 08:56 PM

QUOTE(Diegoratt @ Feb 11 2008, 09:14) <{POST_SNAPBACK}>
Someone must put this solution in the MDC page.


Done.


#8 Diegoratt

Diegoratt

    Newbie

  • Members
  • 8 posts

Posted 12 February 2008 - 12:00 AM

QUOTE(grimholtz @ Feb 11 2008, 19:56) <{POST_SNAPBACK}>
Done.



Hi grimholtz, I think you made a little mistake.

CODE
In XUL, define the string bundle:

<stringbundleset id="stringbundleset">
   <stringbundle id="myext-strings" src="chrome://myext/locale/myext.properties"/>
</stringbundleset>

myext.properties contains name/value pairs such as:

invalid.url=The URL specified for automatic proxy configuration is not a valid URL.

These properties can be accessed from JavaScript in the following manner:

var common = Components.classes["@mozilla.org/intl/stringbundle;1"]
   .getService(Components.interfaces.nsIStringBundleService)
   .createBundle("chrome://myext/locale/myext.properties");

function getLocalizedMessage(msg) {
   return common.GetStringFromName(msg);
}

alert(getLocalizedMessage("invalid.url"));


With that code Is not necesary the stringbundle in the XUL file. In fact is a way to avoid that step. Although nice work putting it on MDC.

#9 ericjung

ericjung

    Advanced Member

  • Members
  • 235 posts

Posted 12 February 2008 - 01:59 AM

QUOTE(Diegoratt @ Feb 12 2008, 00:00) <{POST_SNAPBACK}>
Hi grimholtz, I think you made a little mistake.


Fixed. And I made some nice enhancements, too. Have a look.


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users