Change Locale in Velocity in Liferay theme - liferay

I am using velocity in liferay theme.
In init_custom.vm file I want to include CSS for both English and Arabic.
Which CSS works where it depends on present locale selected?
Like:
If(locale=="eng"){
my eng css
}
else{
my arbic css
}
How do I write this in velocity?
Please give some idea or details.

The answer was already given in this topic. This is what you'll need to do:
Put this line in your init.vm
#set ($language_id = $themeDisplay.getLanguageId())
Then you can add condition like following:
#If($language_id == "en_US")
my eng css
#else
my arbic css
#end
And add $language_id as a class in your html or body tag in portal_normal.vm. You'll get a language ID like "en_US", here's a list.

Related

Add alternate tag to individual pages in liferay

I want to add a meta tag in my each individual page of life ray. I am could only find meta tag with description which is in the SEO of the pages in Control panel.
<link rel="alternate" href="http://example.com/xyz/abc" hreflang="en-au" />
Please help me out with this. I am unable to find an answer to this.
The best and permanent way is to create custom theme (or modify default one) and add general meta-tags, javascript / css files (which you want to appear on each page of portal) through head section of theme's portal_normal.vm file. However, if you want to restrict these tags for specific page(s) you can still do it through Velocity Objects like public / private layout, admin / user difference, current page name / URL, there are numerous options available.
If you are new to theme design, you can start with: Creating Liferay Themes
The other possible and quick way is to add js / css dynamically using javascript / jQuery as following:
if you are using pure javascript:
window.onload = function(){
loadjscssfile("myscript.js", "js");
loadjscssfile("javascript.php", "js");
loadjscssfile("mystyle.css", "css");
}
Or you can use jQuery:
jQuery(function(){
loadjscssfile("myscript.js", "js");
loadjscssfile("javascript.php", "js");
loadjscssfile("mystyle.css", "css");
});
general method:
function loadjscssfile(filename, filetype){
if (filetype=="js"){
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
}
else if (filetype=="css"){
var fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if (typeof fileref!="undefined")
document.getElementsByTagName("head")[0].appendChild(fileref);
}
Reference: http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml
Add above code to the javascript section of the root of your pages.

Change language of web content in liferay

I would like to change language of web content in liferay. So I tried to change language programatically from en_EN to sk_SK (Slovak) this way:
$themeDisplay.getLanguageId() // there it is en_EN
$themeDisplay.setLanguageId('sk_SK')
$themeDisplay.getLanguageId() // there it is sk_SK
same it is with this code
#set ($locale=$localeUtil.fromLanguageId("sk_SK"))
$themeDisplay.setLocale($locale)
$themeDisplay.getLocale()
But the web content didn't change. Do you know why?
If this is not clear I will try to give you an example: On the top of web-page are usually some language buttons, so you can switch between more languages. I want to do same in liferay. So I have one WebContent with more translations and with this buttons I want to change language/translation of webContent.
On the top of web-page are usually some language buttons, so you can
switch between more languages. I want to do same in liferay. So I have
one WebContent with more translations and with this buttons I want to
change language/translation of webContent.
If i'm not wrong Liferay already provide such functionality out-of-the-box. You don't need to do it with pragmatically.
Use Liferay's navigation portlet to switch the languages.
Thank you, but I would like to add it into my code, into *.vm file. So this is my solution (finally I found it)
<div id="language-portlet">
#set ($VOID = $velocityPortletPreferences.setValue('portlet-setup-show-borders', 'true'))
#set ($VOID = $theme.runtime("82", '', $velocityPortletPreferences.toString()))
#set ($VOID = $velocityPortletPreferences.reset())
</div>
It show possible translations on your web-page. (For me it is flags)
The best solution is to override this property
locale.default.request=false
company.default.locale=sk_SK
is portal-ext.proprties file

Conflicts between Custom theme CSS and Liferay CSS

I am trying to customize the Liferay UI by using custom theme using base as as "_Styled" theme.
I have my own css files which I coped to _diff/css folder of theme and imported them "custom.css" file .However its breaking the presentation of liferay.In my custom CSS I have styles defined for all the standard tags like body,div etc which is impacting the liferay UI too.
How can I resolve this conflict? Thanks in advance!
Quick (and not the best) solution is to remove contents of liferays css file (for example "base.css") and save this empty file in /diff/css/ folder of your theme. This way the base.css will get overriden with your new empty file and thus no styles will get loaded. And your custom.css will be the only stylesheet that is taken into account.
Well, of course it all has an effect on the rest of Liferay as well. Liferay provides quite a bit of the HTML DOM of your page, and if you change the presentation of all of those elements, you'll have to take care to style Liferay's elements too.
Is this a conflict? No. Let's go for the simplest case: You declare div {color:green;}. Of course, now everything, your components as well as Liferay's components, use green text. If you only want to style your own portlets, you might want to specify some portlets: div.portlet-my-own-application {color:green;}
I know that color is a too simple usecase, but I hope it illustrates the solution strategy.
Rather than following Artem Khojoyan's suggestion to override Liferay's base.css, I'd recommend to take a look at the resulting css, what's effective etc., and simplify your own css - adapt it to be used within Liferay - by inspecting the effective CSS for every elements that looks off. Firebug or any of it's relatives are your friend.
I'm afraid, with the details "I'm doing something which has an effect on Liferay UI" there's nothing much more to help you. In fact, I'd hope that what you do has an effect on Liferay's UI... You'll just need to find the proper CSS code
Ideally if your styles are loaded from custom.css, then will overwrite liferay default styles.
In some cases, to overwrite a style in css, you can use !important
for example, liferay default style
body {
background-color: #fff
}
You can specify your style to consider irrespective of order of loading
body {
background-color: red !important;
}

Changing portal_normal.vm in Liferay

Right now I am using the default portal_normal.vm to display my page. I have made some changes to custom.css to give some look to the theme (JUst changed the colour of the background)
I have made some changes to the portal_normal.vm.
What if I want to give a complete different look to my page ? My answer is make changes to the portal_normal.vm page.
MY question is :
portal_normal.vm has some variables and stuff like:
<html class="$root_css_class" dir="#language ("lang.dir")" lang="$w3c_language_id">
I would like to know where does portal_normal.vm get these values from? By values I mean values of variables like $root_css_class ? Usually velocity gets these values from a java class. So in liferay theme, from where does portal_normal.vm get these values from? Is it ok to change the portal_normal.vm file completely?
Yes you can change any thing in portal_noraml.vm file and you design your own look.
Here to change portal_normal.vm we will use theme. in _diff/templates place classic theme portal_normal.vm and do changes.
All velocity variable are declared in init.vm file and we have another file called inti_custom.vm for declare our own velocity related variables.
Most of the velocity variables which required for portal already defined by liferay people we just use those.
Use init_custom.vm in _diff/templates folder for custom velocity varibles.
https://github.com/liferay/liferay-portal/blob/6.2.x/portal-web/docroot/html/themes/_unstyled/templates/init.vm
Most of the variables already loaded by velocity engine so max we dont need to create new variables in velocity engine.
You can create theme in liferay and provide customization in portal_normal.vm.
There is init.vm file where some of the variables are declared and also VelocityVariablesImpl.java is the class where vm variables are defined.

Is there a function in Orchard CMS that will retrieve the Theme directory?

I have images in my theme (in the Content/ directory) and I want to show them in different places.
My current approach is : <img src="#Url.Content("~/Themes/MyTheme/Content/Images/image.gif")" />
This works, but is not very maintainable (what if I want to switch themes, etc).
Is there an built in method, something like GetCurrentThemeDirectory() that would return the directory so I could do or something like that?
Edit: from mdm's comment, I realize that changing the theme isn't a valid concern. I really just want to avoid typing out the url for every reference
Where are you referencing the image from? Module? Another theme?
If it is from the theme that has the image, then you don't need to worry about switching themes. If it is from another theme, then the image should be a part of the theme. If it is from a module, then it would make sense to store the image as part of the module or override it in the theme (see below).
If you wanted to have the image as part of the theme, then you could have the module return a 'default' shape and then override that in the theme. There really shouldn't be any reason to reference the theme's images from a module or vice versa.
Edit after your edit
In the themes I've written, I've followed what the Orchard authors do. Rather than using <img> tags, images are placed in Styles/images. They can then be referenced using the CSS background-image attribute.
In Views/Branding.cshtml:
<div id="header"></div>
<h1 id="branding">#WorkContext.CurrentSite.SiteName</h1>
And then in site.css:
#header {
/* snip */
background-image: url(images/header.png);
/* snip */
}
Themes/TheAdmin/site.css contains plenty more examples of this method.

Resources