2

With LockerService activated, $A.util.format is no longer available, whereas it is in the official documentation.

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/labels_dynamic.htm , paragraph "Dynamically Replacing Label Parameters"

Dear SalesForce, please could you correct that ?

Many thanks & best regards

2 Answers 2

2

$A.util.format() was never marked @platform and as such was never meant to be exposed - LS only publishes supported APIs. The documentation in question was adapted from the open source Aura project and should not have referenced $A.util.format() wrt Lightning Platform.

We are discussing this now - and as soon as I get confirmation that the Aura Framework team is signed up to support this as a public API we will get it exposed in Locker.

4
  • Ok .... waiting ! :) Commented Oct 10, 2016 at 8:11
  • The team that owns the decision is still discussing it and I have reiterated the urgency. Commented Oct 10, 2016 at 12:25
  • 1
    The Aura team has indicated that $A.util.format() is not a publicly supported method. An alternative is to use the replace() function in JavaScript. format() is supported in component markup. I'm a tech writer on the Aura team. It was my mistake in exposing $A.util.format() in the doc and I will be removing it soon. The method is intended for internal usage within the framework. Apologies for the confusion. Commented Oct 10, 2016 at 21:44
  • So basically I have to make server calls just to build a label containing variables ? Commented Oct 26, 2016 at 14:15
0

No server call is required:

$A.util.format("Hello {0}!", "world")  

is equivalent to

"Hello {0}!".replace("{0}","world")

Even a variation of Crockford's supplant method can be used:

function supplant(s, o) {
    return typeof s === 'string' && s.replace(
        /\{([^{}]*)\}/g,
        function (a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
        }
    );
}

Use it with an array or replacements:

supplant("Hello {0}!", ["world"])  

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .