diff options
author | Mika Murtojarvi <mika@vaadin.com> | 2014-12-08 17:57:38 +0200 |
---|---|---|
committer | Mika Murtojarvi <mika@vaadin.com> | 2014-12-09 16:43:04 +0200 |
commit | db0403214329e77624caf6e592016a80e6195f6a (patch) | |
tree | bb5aff4d18910f556e888b942ae8a751a7f2b127 /server | |
parent | 12e5d620ab2ce885590a280d2c19e230cc083bb6 (diff) | |
download | vaadin-framework-db0403214329e77624caf6e592016a80e6195f6a.tar.gz vaadin-framework-db0403214329e77624caf6e592016a80e6195f6a.zip |
Handle locale as a special case (#7749).
This cleans the generated html by not writing the locale to every tree
node.
Change-Id: I7a3c8300ee7726e22196c6cbc659fed8b78e3bd6
Diffstat (limited to 'server')
-rw-r--r-- | server/src/com/vaadin/ui/AbstractComponent.java | 43 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/tests/layoutparser/TestLocale.java | 153 |
2 files changed, 195 insertions, 1 deletions
diff --git a/server/src/com/vaadin/ui/AbstractComponent.java b/server/src/com/vaadin/ui/AbstractComponent.java index 11e24a306e..1b7eeadd87 100644 --- a/server/src/com/vaadin/ui/AbstractComponent.java +++ b/server/src/com/vaadin/ui/AbstractComponent.java @@ -938,6 +938,12 @@ public abstract class AbstractComponent extends AbstractClientConnector } else { explicitImmediateValue = null; } + // handle locale + if (attr.hasKey("locale")) { + setLocale(getLocaleFromString(attr.get("locale"))); + } else { + setLocale(null); + } // handle width and height readSize(attr, def); // handle component error @@ -965,6 +971,35 @@ public abstract class AbstractComponent extends AbstractClientConnector } /** + * Constructs a Locale corresponding to the given string. The string should + * consist of one, two or three parts with '_' between the different parts + * if there is more than one part. The first part specifies the language, + * the second part the country and the third part the variant of the locale. + * + * @param localeString + * the locale specified as a string + * @return the Locale object corresponding to localeString + */ + private Locale getLocaleFromString(String localeString) { + if (localeString == null) { + return null; + } + String[] parts = localeString.split("_"); + if (parts.length > 3) { + throw new RuntimeException("Cannot parse the locale string: " + + localeString); + } + switch (parts.length) { + case 1: + return new Locale(parts[0]); + case 2: + return new Locale(parts[0], parts[1]); + default: + return new Locale(parts[0], parts[1], parts[2]); + } + } + + /** * Toggles responsiveness of this component. * * @since 7.4 @@ -1171,7 +1206,7 @@ public abstract class AbstractComponent extends AbstractClientConnector private static final String[] customAttributes = new String[] { "width", "height", "debug-id", "error", "width-auto", "height-auto", "width-full", "height-full", "size-auto", "size-full", - "responsive", "immediate" }; + "responsive", "immediate", "locale" }; /* * (non-Javadoc) @@ -1195,6 +1230,12 @@ public abstract class AbstractComponent extends AbstractClientConnector if (explicitImmediateValue != null) { design.attr("immediate", explicitImmediateValue.toString()); } + // handle locale + if (getLocale() != null + && (getParent() == null || !getLocale().equals( + getParent().getLocale()))) { + design.attr("locale", getLocale().toString()); + } // handle size writeSize(attr, def); // handle component error diff --git a/server/tests/src/com/vaadin/tests/layoutparser/TestLocale.java b/server/tests/src/com/vaadin/tests/layoutparser/TestLocale.java new file mode 100644 index 0000000000..3df72554ad --- /dev/null +++ b/server/tests/src/com/vaadin/tests/layoutparser/TestLocale.java @@ -0,0 +1,153 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * 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 com.vaadin.tests.layoutparser; + +import java.util.Locale; + +import junit.framework.TestCase; + +import org.apache.commons.lang3.LocaleUtils; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.DocumentType; +import org.jsoup.nodes.Element; + +import com.vaadin.ui.Button; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.ui.declarative.LayoutHandler; + +/** + * Tests the handling of the locale property in parsing and html generation. + * + * @since + * @author Vaadin Ltd + */ +public class TestLocale extends TestCase { + DesignContext ctx; + + @Override + public void setUp() { + ctx = new DesignContext(); + } + + /* + * Checks that when the html corresponding to a component hierarchy is + * constructed, the result only contains locale attributes for a component + * if its locale differs from that of its parent. + */ + public void testHtmlGeneration() { + // create a component hierarchy + VerticalLayout vLayout = new VerticalLayout(); + vLayout.setLocale(Locale.US); + HorizontalLayout hLayout = new HorizontalLayout(); + hLayout.setLocale(Locale.ITALY); + vLayout.addComponent(hLayout); + Button b1 = new Button(); + b1.setLocale(Locale.ITALY); + Button b2 = new Button(); + b2.setLocale(Locale.US); + hLayout.addComponent(b1); + hLayout.addComponent(b2); + HorizontalLayout hlayout2 = new HorizontalLayout(); + hlayout2.setLocale(Locale.US); + vLayout.addComponent(hlayout2); + Label l = new Label(); + l.setLocale(Locale.US); + hlayout2.addComponent(l); + Label l2 = new Label(); + l2.setLocale(Locale.CANADA); + hlayout2.addComponent(l2); + ctx.setComponentRoot(vLayout); + // create the html tree corresponding to the component hierarchy + Document doc = LayoutHandler.createHtml(ctx); + // check the created html + Element body = doc.body(); + Element evLayout = body.child(0); + assertEquals("Wrong locale information.", "en_US", + evLayout.attr("locale")); + Element ehLayout = evLayout.child(0); + assertEquals("Wrong locale information.", "it_IT", + ehLayout.attr("locale")); + Element eb1 = ehLayout.child(0); + assertTrue( + "The element should not have a locale specification, found locale " + + eb1.attr("locale"), "".equals(eb1.attr("locale"))); + Element eb2 = ehLayout.child(1); + assertEquals("Wrong locale information.", "en_US", eb2.attr("locale")); + Element ehLayout2 = evLayout.child(1); + assertTrue( + "The element should not have a locale specification, found locale " + + ehLayout2.attr("locale"), + "".equals(ehLayout2.attr("locale"))); + Element el1 = ehLayout2.child(0); + assertTrue( + "The element should not have a locale specification, found locale " + + el1.attr("locale"), "".equals(el1.attr("locale"))); + Element el2 = ehLayout2.child(1); + assertEquals("Wrong locale information.", "en_CA", el2.attr("locale")); + } + + /* + * Checks that the locale of a component is set when the html element + * corresponding to the component specifies a locale. + */ + public void testParsing() { + // create an html document + Document doc = new Document(""); + DocumentType docType = new DocumentType("html", "", "", ""); + doc.appendChild(docType); + Element html = doc.createElement("html"); + doc.appendChild(html); + html.appendChild(doc.createElement("head")); + Element body = doc.createElement("body"); + html.appendChild(body); + Element evLayout = doc.createElement("v-vertical-layout"); + evLayout.attr("locale", "en_US"); + body.appendChild(evLayout); + Element ehLayout = doc.createElement("v-horizontal-layout"); + evLayout.appendChild(ehLayout); + Element eb1 = doc.createElement("v-button"); + eb1.attr("locale", "en_US"); + ehLayout.appendChild(eb1); + Element eb2 = doc.createElement("v-button"); + eb2.attr("locale", "en_GB"); + ehLayout.appendChild(eb2); + Element eb3 = doc.createElement("v-button"); + ehLayout.appendChild(eb3); + + // parse the created document and check the constructed component + // hierarchy + VerticalLayout vLayout = (VerticalLayout) LayoutHandler.parse( + doc.toString()).getComponentRoot(); + assertEquals("Wrong locale.", LocaleUtils.toLocale("en_US"), + vLayout.getLocale()); + HorizontalLayout hLayout = (HorizontalLayout) vLayout.getComponent(0); + assertEquals("The element should have the same locale as its parent.", + vLayout.getLocale(), hLayout.getLocale()); + Button b1 = (Button) hLayout.getComponent(0); + assertEquals("Wrong locale.", LocaleUtils.toLocale("en_US"), + b1.getLocale()); + Button b2 = (Button) hLayout.getComponent(1); + assertEquals("Wrong locale.", LocaleUtils.toLocale("en_GB"), + b2.getLocale()); + Button b3 = (Button) hLayout.getComponent(2); + assertEquals( + "The component should have the same locale as its parent.", + hLayout.getLocale(), b3.getLocale()); + } +}
\ No newline at end of file |