How to read a specific language.properties file? - liferay-7

Here's my problem. I need to generate PDF in particular language chosen by user. Some informations are in language.properties files and I cannot manage to read other files than the current site language.
For example current language is Spanish and User select German. I'd like to read language_de.properties.
I've tried to use:
String Lang=LanguageUtil.get(LanguageUtil.getLocale('de'), "Mykey");
but without sucess, it always returns "Mykey"
If I use :
String Lang=LanguageUtil.get(HttpServletRequest, "Mykey");
It works, but It's the site language and not the one I want.
Any Ideas?

One option is that you have forgotten to define de_DE in your portal-ext.properties for locales.enabled:
#
# Specify the locales that are enabled by default.
#
locales.enabled=en_US,de_DE,es_ES
Another possibility is that the locale is not enabled for your portal instance. You should check that in the admin UI of your instance.
In both cases LanguageUtil.getLocale('de') will return null - and this will return the given key from LanguageUtil.get. You can verify that by printing the result of LanguageUtil.getLocale('de') and LanguageUtil.getLocale('de_DE') (should both be null). Just add the locale to the enabled ones and you are good.
A third option applies, if you have defined the messages in the resource bundle of your portlet (usually you define the messages in a hook or OSGI bundle to use them in more than one portlet). In that case LanguageUtil.get(Locale, String) doesn't know which portlet you are in and can't access the resource bundle of that portlet. That's the difference to LanguageUtil.get(HttpServletRequest, String), which retrieves the portlet from the request.
In that case you have to retrieve the resource bundle from the config given to your Portlet.init(PortletConfig) method. Or you mimic the implementation of LanguageUtil.get(HttpServletRequest, String) with a locale specific parameter:
public class MyLanguageUtil {
public static String get(HttpServletRequest request, Locale locale, String key) {
if (request == null) {
return key;
}
PortletConfig portletConfig = (PortletConfig) request.getAttribute("javax.portlet.config");
if (portletConfig != null) {
ResourceBundle resourceBundle = portletConfig.getResourceBundle(locale);
if (resourceBundle.containsKey(key)) {
return LanguageUtil.get(resourceBundle, key, key);
}
}
return LanguageUtil.get(locale, key);
}
}

ok got it to work.
I added a class for each language in my module language
import java.util.Enumeration;
import java.util.ResourceBundle;
import org.osgi.service.component.annotations.Component;
import com.liferay.portal.kernel.language.UTF8Control;
#Component(
property = { "language.id=es_ES" },
service = ResourceBundle.class
)
public class EsResourceBundle extends ResourceBundle {
#Override
protected Object handleGetObject(String key) {
return _resourceBundle.getObject(key);
}
#Override
public Enumeration<String> getKeys() {
return _resourceBundle.getKeys();
}
private final ResourceBundle _resourceBundle = ResourceBundle.getBundle(
"content.Language_es_ES", UTF8Control.INSTANCE);}
thanks for help

For those who need this to be a little more generic.
Add a small class to your portlet that accesses the portlets ResourceBundle:
public class [MyPortlet]LanguageUtil {
public static String get (Locale locale, String key) {
ResourceBundle resourceBundle = ResourceBundle.getBundle("content.Language", locale, UTF8Control.INSTANCE);
return LanguageUtil.get(resourceBundle, key);
}
public static String format (Locale locale, String pattern, Object[] arguments) {
ResourceBundle resourceBundle = ResourceBundle.getBundle("content.Language", locale, UTF8Control.INSTANCE);
return LanguageUtil.format(resourceBundle, pattern, arguments);
}
}
and export it with the help of the bnd.bnd file of the portlet like:
Export-Package: [path.to.my.util]
This way it can be used in OSGI modules and regular JAVA classes for example like this:
[MyPortlet]LanguageUtil.get(serviceContext.getLocale(), "my-language-string");

Related

How can I add language setting entries to existing CDT eclipse projects?

I am following the steps found here to try to add build settings to files in existing Eclipse CDT projects using the LanguageSettingsProvider extension point, but my settings provider doesn't seem to show in the UI, and its methods aren't queried for settings.
I previously succeeded in adding settings to a project using an external settings provider, but I couldn't find a way to add file-specific settings.
I have implemented a subclass of LanguageSettingsSerializableProvider (let's call it MyProvider), and added it to my plugin.xml thus:
<extension
point="org.eclipse.cdt.core.LanguageSettingsProvider">
<provider
class="com.example.MyProvider"
id="MyProvider_id"
name="I would like to see this in the UI">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
</extension>
The class is implemented approximately thus:
public class MyProvider
extends LanguageSettingsSerializableProvider
implements ILanguageSettingsProvider,
IResourceChangeListener,
ILanguageSettingsEditableProvider,
ILanguageSettingsBroadcastingProvider {
/** The ID of this settings provider */
public static final String MY_PROVIDER_ID = "MyProvider_id"; //$NON-NLS-
/**
* Constructor. Initialises super class with appropriate values.
*/
public MyProvider() {
super( MY_PROVIDER_ID , Messages.UiLabel );
}
#Override
public String getId() {
return MY_PROVIDER_ID ;
}
#Override
public String getName() {
return Messages.UiLabel;
}
#Override
public List<ICLanguageSettingEntry> getSettingEntries(ICConfigurationDescription cfgDescription, IResource resource, String languageId) {
//breakpoint on this line that never gets hit...
return super.getSettingEntries( cfgDescription, resource, languageId );
}
...
}
The real implementation contains some other logic to actually create the settings entries, including registering itself as a resource change listener. The resourceChanged code works fine, calls setSettingsEntries, and then serializeSettings. However, getSettingEntries is never called to obtain these settings.
Is there something I'm missing?
My guess is the plugin.xml is lacking something, but I'm not sure what. There are paragraphs of guidance in the various interfaces that the class implements, but one simple working example would be worth more than a thousand words. I've tried looking at the xml for the built-in settings providers (e.g. GCCBuiltinCompilerSettingsMinGW), but they're defined alongside a lot of other parts of CDT, and it's hard to tell which bits are relevant to my use case.
What I ended up doing was:
someMethod {
final IProject myProject = getProjectFromSomewhere();
final ICProjectDescription projDesc = CoreModel.getDefault().getProjectDescription( myProject );
for (ICConfigurationDescription config : projDesc.getConfigurations()) {
try {
ensureMySettingsProvidedFor( (ILanguageSettingsProvidersKeeper) config );
} catch (ClassCastException e) {
logger.log( Level.WARNING, "Unexpected class was not a keeper of language settings:" + config.getClass().getName(), e );
}
}
}
/**
* Will ensure my settings provider is registered as a settings provider for...
* #param settingsKeeper ...the given configuration.
*/
public static void ensureMySettingsProvidedFor(final ILanguageSettingsProvidersKeeper settingsKeeper) {
if (settingsKeeper instanceof CConfigurationDescriptionCache) {
logger.log(Level.SEVERE, "Got non-writable cached settings. We can't update this!! "
+ "How do we get a writeable version?");
return;
}
for ( ILanguageSettingsProvider provider : settingsKeeper.getLanguageSettingProviders() ) {
if (MyProvider.MY_PROVIDER_ID.equals( provider.getId() )) {
return;
}
}
addMyProvider( settingsKeeper );
}
/**
* Adds my language settings provider to the given configuration by means of its ID.
* #param settingsKeeper The existing configuration.
*/
private static void addMyProvider( final ILanguageSettingsProvidersKeeper settingsKeeper ) {
List<String> ids = new ArrayList<String>();
for ( ILanguageSettingsProvider provider : settingsKeeper.getLanguageSettingProviders() ) {
ids.add( provider.getId() );
}
ids.add(MyProvider.MY_PROVIDER_ID);
List<ILanguageSettingsProvider> newProviders = LanguageSettingsManager.createLanguageSettingsProviders( ids.toArray( new String[ids.size()] ) );
settingsKeeper.setLanguageSettingProviders(newProviders);
}
It doesn't feel like the best answer, but it seems to work most of the time.
One problem is knowing when to do this such that it happens for all projects that don't yet have the provider, but doesn't get called repeatedly, or too often.
Another problem is that we sometimes get a CConfigurationDescriptionCache, and then the code can't do anything.
I'm open to better solutions.

Eclipse Project Explorer filter for XML files

I have many XML files in the workbench Project Explorer, each one an instance of one of ten different ecore models. For each ecore model I would like to contribute a commonFilter to the navigator's navigatorContent extension point to enable the user to show or hide the corresponding XML files. These are external tool files so there is not a way to identify the content merely by observing the file name or the xml extension, and renaming is not feasible. Using perhaps a class deriving from org.eclipse.jface.viewers.ViewerFilter, what is the best way to identify which of the ecore models the XML file contains? I presume there is a simple way to do this with EMF resources, or with EcoreUtil, or with adapters, but I haven't found a successful technique. Alternatively, a way to do this directly from the extension point's filterExpression or the viewer's viewerContentBinding would be fine. All of the genmodel-derived plugins are available for the various ecore models.
package com.my.navigator;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
public class MyViewerFilter extends ViewerFilter {
public MyViewerFilter() {
}
#Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
if ( element instanceof IFile ) {
IFile file = (IFile)element;
// check whether file is one of our ecore models...
// ...
}
return true;
}
}
You could use the org.eclipse.core.contenttype.contentTypes to define new content types for your file.
Use the describer argument of the content type definition to specify a 'content describer' class which can check that the XML file meets your requirements. There is already an XMLContentDescriber class that you can use as a basis for the describer.
For example this is the content type definition for Ant build.xml files:
<extension
point="org.eclipse.core.contenttype.contentTypes">
<content-type
id="antBuildFile"
name="%antBuildFileContentType.name"
base-type="org.eclipse.core.runtime.xml"
file-names="build.xml"
file-extensions="macrodef,ent,xml,ant"
priority="normal">
<describer
class="org.eclipse.ant.internal.core.contentDescriber.AntBuildfileContentDescriber">
</describer>
</content-type>
</extension>
and this is the Ant content describer to give you a rough idea of what you can:
public final class AntBuildfileContentDescriber extends XMLContentDescriber implements IExecutableExtension {
private int checkCriteria(InputSource contents) throws IOException {
AntHandler antHandler = new AntHandler();
try {
if (!antHandler.parseContents(contents)) {
return INDETERMINATE;
}
}
catch (SAXException e) {
// we may be handed any kind of contents... it is normal we fail to parse
return INDETERMINATE;
}
catch (ParserConfigurationException e) {
// some bad thing happened - force this describer to be disabled
String message = "Internal Error: XML parser configuration error during content description for Ant buildfiles"; //$NON-NLS-1$
throw new RuntimeException(message);
}
// Check to see if we matched our criteria.
if (antHandler.hasRootProjectElement()) {
if (antHandler.hasProjectDefaultAttribute() || antHandler.hasTargetElement() || antHandler.hasAntElement()) {
// project and default attribute or project and target element(s)
// or project and top level ant element(s) (classpath, import, macrodef, path, property, taskdef, typedef)
return VALID;
}
// only a top level project element...maybe an Ant buildfile
return INDETERMINATE;
}
return INDETERMINATE;
}
#Override
public int describe(InputStream contents, IContentDescription description) throws IOException {
// call the basic XML describer to do basic recognition
if (super.describe(contents, description) == INVALID) {
return INVALID;
}
// super.describe will have consumed some chars, need to rewind
contents.reset();
// Check to see if we matched our criteria.
return checkCriteria(new InputSource(contents));
}
#Override
public int describe(Reader contents, IContentDescription description) throws IOException {
// call the basic XML describer to do basic recognition
if (super.describe(contents, description) == INVALID) {
return INVALID;
}
// super.describe will have consumed some chars, need to rewind
contents.reset();
// Check to see if we matched our criteria.
return checkCriteria(new InputSource(contents));
}
#Override
public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
// do nothing
}
}

How can I use values from Eclipse Preference Pages to initialize external tool parameters?

I've created Eclipse Plugin from default template with Preference Pages. There are some preference parameters, two of them are String. I also created custom External Tool launch configuration in this Plugin and I use one of String parameters (let it be P_PATH) mentioned before to initialize one of tool's fields by default (using SetDefaults method). The problem is that it doesn't work as I suggested. When I launch this plugin (in another Eclipse instance) I go to Window->Preferences->Sample Preferences where I can edit and save field values. But after I'm done (when i enter something to field connected with P_PATH value there) I create new external tool of selected type and String parameter of selected field (the one tied to P_PATH, let it be "File Path") is inialized by the value that was specified in Plugin, not the one I entered into Preference Pages form. So, I want String value I enter in Preference Pages to be passed to External Tool as one of its parameters and when I create new External Tool of selected type it should be there (as default). How can I do that? I tied one External Tool field and one Preference Pages field to the same string parameter but looks like it's not properly passed to External Tool after all.
Added some code, there are three classes for Preference Pages and the forth one is for Launch Configuration Tabs. Here is only relevant code:
public class PreferenceConstants {
public static final String P_PATH = "pathPreference";
}
public class PreferenceInitializer extends AbstractPreferenceInitializer {org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer#initializeDefaultPreferences()
public void initializeDefaultPreferences() {
store.setDefault(PreferenceConstants.P_PATH,"Default value");
}
}
public class PreferencePage
extends FieldEditorPreferencePage
implements IWorkbenchPreferencePage {
public PreferencePage() {
super(GRID);
setPreferenceStore(Activator.getDefault().getPreferenceStore());
setDescription("A demonstration of a preference page implementation");
}
public void createFieldEditors() {
addField(new FileFieldEditor(PreferenceConstants.P_PATH,
"&Console compiler path:", getFieldEditorParent()));
}
public void init(IWorkbench workbench) {
}
}
public class LaunchConfigurationTabs extends AbstractLaunchConfigurationTabGroup {
#Override
public void setDefaults(ILaunchConfigurationWorkingCopy configuration){
configuration.setAttribute("org.eclipse.ui.externaltools.ATTR_LOCATION", PreferenceConstants.P_PATH);
}
}
The line:
configuration.setAttribute("org.eclipse.ui.externaltools.ATTR_LOCATION", PreferenceConstants.P_PATH);
Just sets the attribute value to 'pathPreference' - this does not do anything to look up the value in the preferences.
You can look up the preference value at that point:
IPreferenceStore prefStore = Activator.getDefault().getPreferenceStore();
String value = prefStore.getString(PreferenceConstants.P_PATH);
configuration.setAttribute("org.eclipse.ui.externaltools.ATTR_LOCATION", value);
I don't think you can do anything that will make the attribute value automatically update if the preference changes.

Set specified locale in wicket where default(for user) are not aviable

I have properties files oriented for specific language only (page_ru_RU.prop and page_en_GB.prop). Where on this page came user with locale en US - he got exception, because no proper locale found.
Can i in this way make somwhere check to manual set locale if user dont have any aviable locale without make default locale (page.prop)
If I am not clear about, please tell me.
If you create a properties file with just the language only, it will be used for all variants: so instead of page_en_GB.properties, you can have a page_en.properties, which will be used for all en_* sublocales.
You can also have a plain page.peroperties, which will be used for everyone who doesn't fit into a more specific category.
Update: Take two, how to take control of Locale selection completely.
Locale in Wicket is stored in the Session object, it can be manipulated using the getLocale() and setLocale() method. This alone might give you a solution, but you can also intercept calls to these methods to override locale selection automaticlly.
//this is your application class
public class MyApplication extends WebApplication {
#Override
public Session newSession( Request req, Response res ) {
return new MySession( req )
}
}
//this is your session class
public class MySession extends WebSession {
public MySession( Request req ) {
super( req );
}
#Override
public void setLocale( Locale locale ) {
// your locale substitution code goes here, for example:
if( locale.getLanguage().equals( "en" ) ) {
super.setLocale( new Locale( "en", "GB" ) );
}
}
}
Is this the sort of thing you're after?

Is there a way to capitalize the first letter of a value of a variable in Eclipse (Helios) code templates

I have a code template with a variable and I would like to capitalize(just the first letter) the value of this variable only in some occurrences. Is there a way to do this?
The template code is as follows - I would like to capitalize Property Name in my function names...
private $$${PropertyName};
${cursor}
public function get${PropertyName}()
{
return $$this->${PropertyName};
}
public function set${PropertyName}($$value)
{
$$this->${PropertyName} = $$value;
}
Please Note: This is a template for use with code templates in the IDE (not in PHP). For details see: http://www.ibm.com/developerworks/opensource/tutorials/os-eclipse-code-templates/index.html
I also want this and tried to build a custom TemplateVariableResolver to do it. (I already have one custom resolver in place that generates new UUIDs a la http://dev.eclipse.org/blogs/jdtui/2007/12/04/text-templates-2/.)
I made a custom resolver bound to capitalize:
public class CapitalizingVariableResolver extends TemplateVariableResolver {
#Override
public void resolve(TemplateVariable variable, TemplateContext context) {
#SuppressWarnings("unchecked")
final List<String> params = variable.getVariableType().getParams();
if (params.isEmpty())
return;
final String currentValue = context.getVariable(params.get(0));
if (currentValue == null || currentValue.length() == 0)
return;
variable.setValue(currentValue.substring(0, 1).toUpperCase() + currentValue.substring(1));
}
}
(plugin.xml:)
<extension point="org.eclipse.ui.editors.templates">
<resolver
class="com.foo.CapitalizingVariableResolver"
contextTypeId="java"
description="Resolves to the value of the variable named by the first argument, but with its first letter capitalized."
name="capitalized"
type="capitalize">
</resolver>
</extension>
that I would use like this: (I am working in Java; I see that you do not appear to be)
public PropertyAccessor<${propertyType}> ${property:field}() {
return ${property};
}
public ${propertyType} get${capitalizedProperty:capitalize(property)}() {
return ${property}.get();
}
public void set${capitalizedProperty}(${propertyType} ${property}) {
this.${property}.set(${property});
}
As of Eclipse 3.5, the problem I am having is that my custom resolver does not get a chance to re-resolve once I've specified a value for the property variable. It appears that the Java Development Tools (Eclipse JDT) do this dependent template variable re-resolution via a mechanism called MultiVariableGuess within the JavaContext (see addDependency()). Unfortunately for us, that mechanism does not seem to be exposed, so I/we can't use it to do the same (without lots of copy-and-paste or other redundant work).
At this point, I am giving up again for a while and will keep typing the leading-lowercase and leading-uppercase names separately into two independent template variables.