2020 * #L%
2121 */
2222
23+ import com .vaadin .flow .component .AttachEvent ;
2324import com .vaadin .flow .component .Component ;
2425import com .vaadin .flow .component .HasElement ;
2526import com .vaadin .flow .component .PropertyDescriptor ;
2627import com .vaadin .flow .component .PropertyDescriptors ;
2728import com .vaadin .flow .component .Synchronize ;
2829import com .vaadin .flow .component .Tag ;
30+ import com .vaadin .flow .component .UI ;
2931import com .vaadin .flow .component .dependency .JsModule ;
3032import com .vaadin .flow .component .dependency .NpmPackage ;
33+ import com .vaadin .flow .function .SerializableConsumer ;
34+ import com .vaadin .flow .internal .JsonSerializer ;
3135import com .vaadin .flow .router .RouterLayout ;
3236
37+ import java .io .Serializable ;
3338import java .util .Objects ;
3439
40+ import elemental .json .JsonObject ;
41+ import elemental .json .JsonType ;
42+
3543/**
3644 * Server-side component for the {@code <vaadin-app-layout>} element. Provides a
3745 * quick and easy way to get a common application layout.
@@ -51,6 +59,77 @@ public class AppLayout extends Component implements RouterLayout {
5159
5260 private Component content ;
5361
62+ private AppLayoutI18n i18n ;
63+
64+ /**
65+ * Gets the internationalization object previously set for this component.
66+ * <p>
67+ * Note: updating the i18n object that is returned from this method will not
68+ * update the the component, unless it is set again using
69+ * {@link AppLayout#setI18n(AppLayoutI18n)}
70+ *
71+ * @return the i18n object. It will be <code>null</code>, if the i18n
72+ * properties are not set.
73+ */
74+ public AppLayoutI18n getI18n () {
75+ return i18n ;
76+ }
77+
78+ /**
79+ * Sets the internationalization properties for this component.
80+ *
81+ * @param i18n
82+ * the internationalized properties, not <code>null</code>
83+ */
84+ public void setI18n (AppLayoutI18n i18n ) {
85+ Objects .requireNonNull (i18n ,
86+ "The I18N properties object should not be null" );
87+ this .i18n = i18n ;
88+
89+ runBeforeClientResponse (ui -> {
90+ if (i18n == this .i18n ) {
91+ setI18nWithJS ();
92+ }
93+ });
94+ }
95+
96+ private void setI18nWithJS () {
97+ JsonObject i18nJson = (JsonObject ) JsonSerializer .toJson (this .i18n );
98+
99+ // Remove properties with null values to prevent errors in web
100+ // component
101+ removeNullValuesFromJsonObject (i18nJson );
102+
103+ // Assign new I18N object to WC, by merging the existing
104+ // WC I18N, and the values from the new AppLayoutI18n instance,
105+ // into an empty object
106+ getElement ().executeJs ("this.i18n = Object.assign({}, this.i18n, $0);" ,
107+ i18nJson );
108+ }
109+
110+ @ Override
111+ protected void onAttach (AttachEvent attachEvent ) {
112+ super .onAttach (attachEvent );
113+
114+ // Element state is not persisted across attach/detach
115+ if (this .i18n != null ) {
116+ setI18nWithJS ();
117+ }
118+ }
119+
120+ private void removeNullValuesFromJsonObject (JsonObject jsonObject ) {
121+ for (String key : jsonObject .keys ()) {
122+ if (jsonObject .get (key ).getType () == JsonType .NULL ) {
123+ jsonObject .remove (key );
124+ }
125+ }
126+ }
127+
128+ private void runBeforeClientResponse (SerializableConsumer <UI > command ) {
129+ getElement ().getNode ().runWhenAttached (ui -> ui
130+ .beforeClientResponse (this , context -> command .accept (ui )));
131+ }
132+
54133 /**
55134 * @see #setPrimarySection(Section)
56135 * @return value for the primarySection property. Default is
@@ -284,4 +363,34 @@ public static Section fromWebcomponentValue(String webcomponentValue) {
284363 : null ;
285364 }
286365 }
366+
367+ /**
368+ * The internationalization properties for {@link AppLayout}
369+ */
370+ public static class AppLayoutI18n implements Serializable {
371+ private String drawer ;
372+
373+ /**
374+ * Gets the text for the `aria-label` attribute on the drawer.
375+ *
376+ * @return the drawer aria-label
377+ */
378+ public String getDrawer () {
379+ return drawer ;
380+ }
381+
382+ /**
383+ * Sets the text for the `aria-label` attribute on the drawer. The
384+ * attribute is set when the drawer is in the overlay mode and announced
385+ * once the drawer is opened.
386+ *
387+ * @param drawer
388+ * the drawer aria-label
389+ * @return this instance for method chaining
390+ */
391+ public AppLayoutI18n setDrawer (String drawer ) {
392+ this .drawer = drawer ;
393+ return this ;
394+ }
395+ }
287396}
0 commit comments