1

How to reproduce and fix error "$Label.namespace.LabelName does not exist" while the label actually exists and namespace is correct but lowerspaced?

There is an interesting bug in Salesforce platform, which is hardly to catch and reproduce, but it is somehow related to user locale in client org where package is installed.

There is Lightning Aura Component, which references some labels in a form

{!$Label.c.LabelName} in Aura component code and $A.get("$Label.c.AnotherLabelName") in Aura Helper Javascript code.

The same package version behaves differently for different user locales and sometimes even for the same user locales.

Sometimes this bug reproduces in form of error "$Label.namespace.LabelName does not exist" while the actual correct namespace is uppercased, but in the error message it appears in lowercase and when I look through compiled Javascript code, it also references lowercased version of package namespace $A.get("$Label.namespace.AnotherLabelName")

When the bug is not reproduced, I can see in compiled Javascript code that it references uppercased version of package namespace $A.get("$Label.NAMESPACE.AnotherLabelName") which seems correct.

When bug is reproduced, users always have different than English (US) user locale (for example, Italy (Italian), Czech (Czech) but language is still English). However, when I set user locale to one of these, I can't reproduce the bug.

How compiled code of Aura Component can differ in different user locales? How namespace prefix can be sometimes compiled to uppercase and sometimes to lowercase? How this can be related to user locale?

  • You have mentioned both $Label.c.LabelName and $Label.namespace.LabelName in your description. I am a bit confused. Is the Aura Component in the namespace? – Phil W Aug 23 '19 at 8:57
  • If you use namespace for a label in Aura Component, Salesforce will automatically remove it and keep 'c' instead of namespace. However, when Salesforce builds a package, it somehow replaces 'c' with namespace and I am struggling to understand when it substitutes correct uppercased version of namespace and when it substitutes lowercased version. – Patlatus Aug 23 '19 at 9:27
  • Hmmm. OK, in my experience I had to use $Label.mynamespace.MyLabel for this to work when the Aura component is part of my package with "mynamespace". I certainly didn't notice any removal of the namespace and replacement by "c" (and if I tried to use "c" it all went horribly wrong). Are you doing SFDX or classic mdapi development? – Phil W Aug 23 '19 at 10:51
  • 1
  • Thanks. This is interesting reading - we never develop on, and pull stuff down from, our orgs (we use SFDX and git with an IDE) which is why we wouldn't have seen it. Doesn't this mean you just need to change your source to use $Label.c.LabelName to avoid your issue? – Phil W Aug 23 '19 at 12:03
1

I don't know how you can reproduce the error, but I have some ideas about how you might be able to fix it...

As per the docs for getting labels in Aura Javascript, static references to labels are evaluated at compile-time and sent to the client along with the rest of the component at run-time. However:

Using $A.get() with a label that can't be determined at runtime means that dynamicLabel is an empty string, and won’t be updated to the retrieved value. Since the label, "$Label.c." + day, is dynamically generated, the framework can’t parse it or send it to the client when the component is requested.

So, if your client happens to have the required label already cached when your component loads, the dynamic reference will work. If not, it will fail. This may be why it works differently for you in different locales.

It's messy, but SF's suggested solution is to use $A.getReference() to force the framework to get the dynamic label from the server:

var labelSubStr = "task_mode_today";
var labelReference = $A.getReference("$Label.c." + labelSubStr);
cmp.set("v.tempLabelAttr", labelReference);

I was having intermittent failures with a dynamic label (no locale complications for me, though), and doing this solved the problem for me.

|improve this answer|||||
  • 1
    In my case I have only static labels. They are referenced statically and they are not dynamically generated. The label {!$Label.c.LabelName} in Aura component code should create static dependency and it should be already cached. – Patlatus Aug 23 '19 at 8:40
  • Fair point, but it might still be worth giving the getReference approach a go. If it isn't getting all the locale labels, that sounds like a bug, but this might be a workaround – Aidan Aug 23 '19 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.