function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tj reddytj reddy 

Accessing Custom Labels dynamically from visualforce components

Hi All,

I have a field label__c where i am storing custom label name related to each record. 
Here i am able to display this in visualforce page by using {!$Label[label__c]}, but i am not able to use same in visualforce components.

Please help me out in achiving this.

Thanks in advance.

 
Best Answer chosen by tj reddy
tj reddytj reddy
I have resolved my issue.
We also need to look at version of visualforce component.

Thanks again for your responses.

All Answers

swati_sehrawatswati_sehrawat
Can you please share the line of code where you are using custom label in visualforce component
tj reddytj reddy
There is no complex logic around this, It is just a simple visualforce component where i want to display custom labels dynamically..
sridhar v 7sridhar v 7
Hi,
I believe while you are referring the field in visualforce, you call it as object.fieldapiname like below.
<apex:page standardController="account">
     {!$Label[Account.test_la__c]}
</apex:page>

Similarly, in a component when i try this it shows me the value correctly. Not sure, if this is what you are looking for.

Component: i am passing the account info from VF Page. i think this could be done without that also i.e. fetch the value in the component itself directly.

<apex:component controller="testcomponent">
<apex:attribute assignTo="{!accountObj}" name="account12" type="Account" description="test"/>
    {!$Label[accountObj.test_la__c]}
</apex:component>

Component Controller:
public class testcomponent { 
    public account accountObj{get;set;}
}

VF including component:
<apex:page standardController="account">
<c:testcomponent account12="{!Account}"></c:testcomponent>
</apex:page>

Please mark this as right if this solution works for you.

Thanks,
tj reddytj reddy
I am not passing this from page. I had a controller associated to component where i am querying my object with label__c field and using the same as you mentioned but its throwing Syntax error. Found '$Label' error
 
tj reddytj reddy
I have resolved my issue.
We also need to look at version of visualforce component.

Thanks again for your responses.
This was selected as the best answer