Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
Liferay extending LanguageUtil.get() method
Hello,
We are using Liferay 6.2 we need to override the LIferay's default LanguageUtil.get() method. LanguageUtil class exist in com.liferay.portal.kernel.language package please let me know if its possible to override this class using ext or is there any better way to change the implementation?
Thanks,
Nishikant Sapkal
We are using Liferay 6.2 we need to override the LIferay's default LanguageUtil.get() method. LanguageUtil class exist in com.liferay.portal.kernel.language package please let me know if its possible to override this class using ext or is there any better way to change the implementation?
Thanks,
Nishikant Sapkal
Um, why does it need to change?
Thanks for response David.
We have lot of values coming from custom language hook which displays throughout the portal and values get changed frequently so every time deployment window is needed on production server. We wanted to make it DB base so that no deployment should be needed.
<liferay-ui:message key="${errorMsg }"/> these tags has been used everywhere in code so don't wanted to change this code instead we want to override the LanguageUtil.get(loacle,key) method so that end code should be as it is.
Hope this clears. Can you please let us know the best way of implementing this.
We have lot of values coming from custom language hook which displays throughout the portal and values get changed frequently so every time deployment window is needed on production server. We wanted to make it DB base so that no deployment should be needed.
<liferay-ui:message key="${errorMsg }"/> these tags has been used everywhere in code so don't wanted to change this code instead we want to override the LanguageUtil.get(loacle,key) method so that end code should be as it is.
Hope this clears. Can you please let us know the best way of implementing this.
Oh, i see.
So you don't want to touch the LanguageUtil class, it's a static class. However, you can use your own LanguageImpl bean registered into the portal's Spring context to replace the default (based upon resource bundle files) with a DB-based alternative.
No muss, no fuss.
So you don't want to touch the LanguageUtil class, it's a static class. However, you can use your own LanguageImpl bean registered into the portal's Spring context to replace the default (based upon resource bundle files) with a DB-based alternative.
No muss, no fuss.
<p>Hello,</p>
<p>I have a similar request (overriding a method in the LanguageUtil class). Can you kindly point me to a wiki or example that shows an actual implementation for registering one's own LanguageImpl bean into the portal's Spring context to replace the default bean? Specifically, I would like to know which file(s) and/or project(s) need to be updated (minus the custom LanguageImpl bean).</p>
<p>Also, this would be for Liferay 6.1.x, if that makes any difference.</p>
<p>Thanks, -Patrick</p>
<p>I have a similar request (overriding a method in the LanguageUtil class). Can you kindly point me to a wiki or example that shows an actual implementation for registering one's own LanguageImpl bean into the portal's Spring context to replace the default bean? Specifically, I would like to know which file(s) and/or project(s) need to be updated (minus the custom LanguageImpl bean).</p>
<p>Also, this would be for Liferay 6.1.x, if that makes any difference.</p>
<p>Thanks, -Patrick</p>
Can you kindly point me to a wiki or example that shows an actual implementation for registering one's own LanguageImpl bean into the portal's Spring context to replace the default bean?
I can't provide you with an actual implementation, but I can describe what that implementation needs to look like.
To do exactly what you've decribed, you would need to create an EXT plugin, which would theoretically allow you to replace the bean for LanguageUtil (see util-spring.xml to see how it was originally defined) with a different bean that sets the language property to your own custom implementation, which would probably look something like this:
You can do this inside of a servlet context listener which you specify in your plugin's web.xml or instead of a global.startup.events portal properties hook. The code sample below is what the injection would look like for a servlet context listener:
I can't provide you with an actual implementation, but I can describe what that implementation needs to look like.
To do exactly what you've decribed, you would need to create an EXT plugin, which would theoretically allow you to replace the bean for LanguageUtil (see util-spring.xml to see how it was originally defined) with a different bean that sets the language property to your own custom implementation, which would probably look something like this:
<beans default-destroy-method="destroy" default-init-method="afterPropertiesSet" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="com.liferay.portal.kernel.language.LanguageUtil" class="com.liferay.portal.kernel.language.LanguageUtil">
<property name="language">
<bean class="com.example.MyCustomLanguageImpl" />
</property>
</bean>
</beans>
If you're trying to avoid using EXT plugins, you can achieve something similar by calling LanguageUtil.setLanguage to replace the underlying implementation of Language (since the LanguageUtil you're looking at comes from the global classloader, so setting static fields works), and then all calls to LanguageUtil will use your implementation instead.You can do this inside of a servlet context listener which you specify in your plugin's web.xml or instead of a global.startup.events portal properties hook. The code sample below is what the injection would look like for a servlet context listener:
package com.example;
import com.liferay.portal.kernel.util.BasePortalLifecycle;
import javax.servlet.ServletContextListener;
public class MyContextListener
extends BasePortalLifecycle
implements ServletContextListener {
@Override
protected void doPortalInit() throws Exception {
_oldLanguage = LanguageUtil.getLanguage();
new LanguageUtil().setLanguage(new MyCustomLanguageImpl());
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
new LanguageUtil().setLanguage(_oldLanguage);
}
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
registerPortalLifecycle();
}
private Language _oldLanguage;
}
The EXT plugin approach allows you to reuse a lot of the original LanguageImpl implementation (because you are in the portal class loader, you can just extend LanguageImpl directly), but the regular plugin approach allows you to redeploy your changes and test your modifications incrementally.
Can't you just implement it by overriding language keys?
https://portal.liferay.dev/docs/7-0/tutorials/-/knowledge_base/t/overriding-language-keys
I mean, instead of returning the keys and text from a resource bundle, you could return it from the database.
https://portal.liferay.dev/docs/7-0/tutorials/-/knowledge_base/t/overriding-language-keys
I mean, instead of returning the keys and text from a resource bundle, you could return it from the database.
@Override
public Enumeration<string> getKeys() {
return someLocalService.getKeys();
}</string>
Can't you just implement it by overriding language keys?
Being able to provide new ResourceBundles was an extension point added to 7.x, so it's tricker to do in 6.1.x where that extension point doesn't exist.
Being able to provide new ResourceBundles was an extension point added to 7.x, so it's tricker to do in 6.1.x where that extension point doesn't exist.
Minhchau,
Thank you for your detailed response. I attempted the Ext plugin approach (lots of guesswork on my part in regards to piecing together the required files, etc) and was able to get the MyCustomLanguageImpl class to replace the Liferay LanguageImpl class. However, something with my implementation is off as I get the following error at server startup:
https://github.com/pmercer/liferay-language-plugin-ext
Also, I did not see how it would be possible to simply extend the Liferay LanguageImpl directly, as I get the following error in the IDE when I try to do so:
Thanks, -Patrick
Thank you for your detailed response. I attempted the Ext plugin approach (lots of guesswork on my part in regards to piecing together the required files, etc) and was able to get the MyCustomLanguageImpl class to replace the Liferay LanguageImpl class. However, something with my implementation is off as I get the following error at server startup:
ERROR [LiferayMethodExceptionEventHandler:34] java.lang.NullPointerException
java.lang.NullPointerException
at com.example.LanguageResources._loadLocale(LanguageResources.java:155)
at com.example.LanguageResources.getMessage(LanguageResources.java:81)
at com.example.MyCustomLanguageImpl.get(MyCustomLanguageImpl.java:366)
at com.example.MyCustomLanguageImpl.get(MyCustomLanguageImpl.java:353)
So, if you get a chance, would you mind taking a look at my Ext plugin and offer some feedback on what I am missing or doing wrong?https://github.com/pmercer/liferay-language-plugin-ext
Also, I did not see how it would be possible to simply extend the Liferay LanguageImpl directly, as I get the following error in the IDE when I try to do so:
public class MyOtherCustomLanguageImpl extends LanguageImpl
[i]Implicit super constructor LanguageImpl() is not visible for default constructor. Must define an explicit constructor[/i]
Also, I have not attempted the servlet context listener approach yet. Basically, my goal is to simply override the LanguageUtil.updateCookie method.Thanks, -Patrick
Patrick Mercer:
Also, I did not see how it would be possible to simply extend the Liferay LanguageImpl directly, as I get the following error in the IDE when I try to do so:
Ah, I didn't realize we gave it a private constructor.
Since your main goal is only to override LanguageImpl, you tried deleting the LanguageResources you have in your com.example package and instead importing the one in com.liferay.portal.language package? It's declared as public, and its config is initialized via util-spring.xml, and using the existing one allows you to not have to copy the same initialization into your own custom one.
Yep, that did the trick. Problem solved. Thank you sir!
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™