From 492e0797b40d7882c5150a1ae0d7b5018f80237e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petter=20Holmstro=CC=88m?= Date: Mon, 29 Feb 2016 13:46:27 +0200 Subject: [PATCH] Added new i18n support classes --- .../spring/i18n/support/Translatable.java | 36 ++++++++ .../i18n/support/TranslatableSupport.java | 60 ++++++++++++++ .../spring/i18n/support/TranslatableUI.java | 82 +++++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 addons/i18n/src/main/java/org/vaadin/spring/i18n/support/Translatable.java create mode 100644 addons/i18n/src/main/java/org/vaadin/spring/i18n/support/TranslatableSupport.java create mode 100644 addons/i18n/src/main/java/org/vaadin/spring/i18n/support/TranslatableUI.java diff --git a/addons/i18n/src/main/java/org/vaadin/spring/i18n/support/Translatable.java b/addons/i18n/src/main/java/org/vaadin/spring/i18n/support/Translatable.java new file mode 100644 index 00000000..f2bd915a --- /dev/null +++ b/addons/i18n/src/main/java/org/vaadin/spring/i18n/support/Translatable.java @@ -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 (petter@vaadin.com) + */ +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); +} diff --git a/addons/i18n/src/main/java/org/vaadin/spring/i18n/support/TranslatableSupport.java b/addons/i18n/src/main/java/org/vaadin/spring/i18n/support/TranslatableSupport.java new file mode 100644 index 00000000..df282a51 --- /dev/null +++ b/addons/i18n/src/main/java/org/vaadin/spring/i18n/support/TranslatableSupport.java @@ -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 (petter@vaadin.com) + */ +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); + } + } + } + +} diff --git a/addons/i18n/src/main/java/org/vaadin/spring/i18n/support/TranslatableUI.java b/addons/i18n/src/main/java/org/vaadin/spring/i18n/support/TranslatableUI.java new file mode 100644 index 00000000..c7d31608 --- /dev/null +++ b/addons/i18n/src/main/java/org/vaadin/spring/i18n/support/TranslatableUI.java @@ -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 (petter@vaadin.com) + */ +public abstract class TranslatableUI extends UI { + + private final TranslatableSupport translatableSupport = new TranslatableSupport(this); + + /** + * {@inheritDoc} + *

+ * 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} + *

+ * 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()); + } +}