-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
addons/i18n/src/main/java/org/vaadin/spring/i18n/support/Translatable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
60 changes: 60 additions & 0 deletions
60
addons/i18n/src/main/java/org/vaadin/spring/i18n/support/TranslatableSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
addons/i18n/src/main/java/org/vaadin/spring/i18n/support/TranslatableUI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |