Skip to content

Commit c8eced1

Browse files
authored
feat: introduce i18n support for side nav flow counterpart (#5208)
1 parent ddefc58 commit c8eced1

3 files changed

Lines changed: 164 additions & 0 deletions

File tree

  • vaadin-side-nav-flow-parent
    • vaadin-side-nav-flow-integration-tests/src
    • vaadin-side-nav-flow/src/main/java/com/vaadin/flow/component/sidenav
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2023 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.sidenav.tests;
17+
18+
import com.vaadin.flow.component.html.Div;
19+
import com.vaadin.flow.component.html.NativeButton;
20+
import com.vaadin.flow.component.sidenav.SideNav;
21+
import com.vaadin.flow.component.sidenav.SideNavItem;
22+
import com.vaadin.flow.router.Route;
23+
24+
@Route("vaadin-side-nav/side-nav-i18n")
25+
public class SideNavI18NPage extends Div {
26+
27+
public SideNavI18NPage() {
28+
SideNav sideNav = new SideNav();
29+
sideNav.addItem(new SideNavItem("Item 1"));
30+
sideNav.addItem(new SideNavItem("Item 2"));
31+
32+
NativeButton setI18n = new NativeButton("Set i18n", e -> sideNav
33+
.setI18n(new SideNav.SideNavI18n().setToggle("Updated")));
34+
setI18n.setId("set-i18n");
35+
36+
add(sideNav, setI18n);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2023 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.vaadin.flow.component.sidenav.tests;
18+
19+
import com.vaadin.flow.component.sidenav.testbench.SideNavItemElement;
20+
import com.vaadin.flow.testutil.TestPath;
21+
import com.vaadin.tests.AbstractComponentIT;
22+
import org.junit.Assert;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.openqa.selenium.By;
26+
27+
import java.util.List;
28+
29+
/**
30+
* Integration tests for the {@link SideNavI18NPage}.
31+
*
32+
* @author Vaadin Ltd.
33+
*/
34+
@TestPath("vaadin-side-nav/side-nav-i18n")
35+
public class SideNavI18NIT extends AbstractComponentIT {
36+
37+
@Before
38+
public void init() {
39+
open();
40+
}
41+
42+
@Test
43+
public void itemsHaveCorrectDefaultI18N() {
44+
List<SideNavItemElement> elements = $(SideNavItemElement.class).all();
45+
elements.forEach(element -> Assert.assertEquals("Toggle child items",
46+
getI18nText(element)));
47+
}
48+
49+
@Test
50+
public void setI18n_i18nIsUpdated() {
51+
$("button").id("set-i18n").click();
52+
53+
List<SideNavItemElement> elements = $(SideNavItemElement.class).all();
54+
elements.forEach(element -> Assert.assertEquals("Updated",
55+
getI18nText(element)));
56+
}
57+
58+
private String getI18nText(SideNavItemElement element) {
59+
return element.getWrappedElement().getShadowRoot()
60+
.findElement(By.id("i18n")).getText();
61+
}
62+
}

vaadin-side-nav-flow-parent/vaadin-side-nav-flow/src/main/java/com/vaadin/flow/component/sidenav/SideNav.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
import com.vaadin.flow.component.dependency.JsModule;
2626
import com.vaadin.flow.component.dependency.NpmPackage;
2727
import com.vaadin.flow.dom.Element;
28+
import com.vaadin.flow.internal.JsonSerializer;
29+
30+
import java.io.Serializable;
31+
import java.util.Objects;
2832

2933
/**
3034
* A side navigation menu with support for hierarchical and flat menus.
@@ -42,6 +46,8 @@ public class SideNav extends SideNavItemContainer implements HasSize, HasStyle {
4246

4347
private Element labelElement;
4448

49+
private SideNavI18n i18n;
50+
4551
/**
4652
* Creates a new menu without any label.
4753
*/
@@ -151,6 +157,35 @@ protected void onAttach(AttachEvent attachEvent) {
151157
checkFeatureFlag();
152158
}
153159

160+
/**
161+
* Gets the internationalization object previously set for this component.
162+
* <p>
163+
* Note: updating the object content that is gotten from this method will
164+
* not update the lang on the component if not set back using
165+
* {@link SideNav#setI18n(SideNavI18n)}
166+
*
167+
* @return the i18n object. It will be <code>null</code>, If the i18n
168+
* properties weren't set.
169+
*/
170+
public SideNavI18n getI18n() {
171+
return i18n;
172+
}
173+
174+
/**
175+
* Updates the i18n settings in the web component. Merges the
176+
* {@link SideNavI18n} settings with the current / default settings of the
177+
* web component.
178+
*
179+
* @param i18n
180+
* the internationalized properties, not <code>null</code>
181+
*/
182+
public void setI18n(SideNavI18n i18n) {
183+
Objects.requireNonNull(i18n,
184+
"The i18N properties object should not be null");
185+
this.i18n = i18n;
186+
getElement().setPropertyJson("i18n", JsonSerializer.toJson(i18n));
187+
}
188+
154189
/**
155190
* Checks whether the SideNav component feature flag is active. Succeeds if
156191
* the flag is enabled, and throws otherwise.
@@ -180,4 +215,33 @@ protected FeatureFlags getFeatureFlags() {
180215
.get(UI.getCurrent().getSession().getService().getContext());
181216
}
182217

218+
/**
219+
* The internationalization properties for {@link SideNav}.
220+
*/
221+
public static class SideNavI18n implements Serializable {
222+
private String toggle;
223+
224+
/**
225+
* The text announced by screen readers when focusing the button for
226+
* toggling child items.
227+
*
228+
* @return the translated expression for toggling child items
229+
*/
230+
public String getToggle() {
231+
return toggle;
232+
}
233+
234+
/**
235+
* Sets the text announced by screen readers when focusing the button
236+
* for toggling child items.
237+
*
238+
* @param toggle
239+
* the translated expression for toggling child items
240+
* @return this instance for method chaining
241+
*/
242+
public SideNavI18n setToggle(String toggle) {
243+
this.toggle = toggle;
244+
return this;
245+
}
246+
}
183247
}

0 commit comments

Comments
 (0)