Skip to content

Commit

Permalink
Added new i18n support classes
Browse files Browse the repository at this point in the history
  • Loading branch information
peholmst committed Feb 29, 2016
1 parent c9b46e3 commit 492e079
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2016 The original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vaadin.spring.i18n.support;

import java.io.Serializable;
import java.util.Locale;

/**
* Interface to be implemented by all components that contain some kind of internationalized content that needs to be
* updated on the fly when the locale is changed.
*
* @see TranslatableSupport
* @author Petter Holmström ([email protected])
*/
public interface Translatable extends Serializable {

/**
* Called when the component should update all of its translatable strings, setting locales, etc. The locale to use
*
* @param locale the new locale to use.
*/
void updateMessageStrings(Locale locale);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2016 The original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vaadin.spring.i18n.support;

import java.util.Locale;

import com.vaadin.ui.Component;
import com.vaadin.ui.HasComponents;
import com.vaadin.ui.UI;

/**
* Implementation of {@link Translatable} intended to be used as a delegate by an owning {@link UI}.
* The {@link #updateMessageStrings(Locale)} method will traverse the entire component hierarchy of the UI and
* update the message strings of any components that implement the {@link Translatable} interface.
*
* @author Petter Holmström ([email protected])
*/
public class TranslatableSupport implements Translatable {

private final UI ui;

/**
* Creates a new {@code TranslatableSupport}.
*
* @param ui the UI that owns the object.
*/
public TranslatableSupport(UI ui) {
this.ui = ui;
}

@Override
public void updateMessageStrings(Locale locale) {
updateMessageStrings(locale, ui);
}

private void updateMessageStrings(Locale locale, Component component) {
if (component instanceof Translatable) {
((Translatable) component).updateMessageStrings(locale);
}
if (component instanceof HasComponents) {
for (Component child : (HasComponents) component) {
updateMessageStrings(locale, child);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2016 The original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vaadin.spring.i18n.support;

import java.util.Locale;

import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.UI;

/**
* Base class intended to make it easier to write UIs that needs to support changing the locale on the fly.
* You are not required to extend this class to be able to use {@link Translatable} components. You can
* easily plug in the {@link TranslatableSupport} into your existing UIs.
*
* @author Petter Holmström ([email protected])
*/
public abstract class TranslatableUI extends UI {

private final TranslatableSupport translatableSupport = new TranslatableSupport(this);

/**
* {@inheritDoc}
* <p>
* This method will also update the message strings of all {@link Translatable} components currently attached
* to the UI.
*
* @see #updateMessageStrings()
*/
@Override
public void setLocale(Locale locale) {
super.setLocale(locale);
updateMessageStrings();
}

/**
* {@inheritDoc}
* <p>
* This implementation will delegate the UI initialization to {@link #initUI(VaadinRequest)}, then update
* the message strings of all {@link Translatable} components.
*
* @see #updateMessageStrings()
*/
@Override
protected void init(VaadinRequest request) {
initUI(request);
updateMessageStrings();
}

/**
* Called by {@link #init(VaadinRequest)} to actually initialize the UI.
*
* @param request
*/
protected abstract void initUI(VaadinRequest request);

/**
* Returns the {@link TranslatableSupport} delegate owned by this UI.
*/
protected TranslatableSupport getTranslatableSupport() {
return translatableSupport;
}

/**
* Updates the message strings of all {@link Translatable} components attached to this UI.
*/
protected void updateMessageStrings() {
getTranslatableSupport().updateMessageStrings(getLocale());
}
}

0 comments on commit 492e079

Please sign in to comment.