-
Notifications
You must be signed in to change notification settings - Fork 191
feat: Add localeSignal() method to UI and VaadinSession #23367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Artur-
wants to merge
6
commits into
main
Choose a base branch
from
current-locale-signal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+314
−7
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d231267
feat: Add localeSignal() method to UI and VaadinSession
Artur- 146ca28
Merge remote-tracking branch 'origin/main' into HEAD
Artur- 6c5d860
feat: Make locale signals writable and use SharedValueSignal for session
Artur- 0b8cfb6
refactor: Make locale signal the single source of truth
Artur- 406f2fa
docs: Clarify localeSignal() behavior differences from setLocale()
Artur- 980cb29
Merge branch 'main' into current-locale-signal
Artur- File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -54,6 +54,8 @@ | |
| import com.vaadin.flow.server.startup.ApplicationConfiguration; | ||
| import com.vaadin.flow.shared.Registration; | ||
| import com.vaadin.flow.shared.communication.PushMode; | ||
| import com.vaadin.signals.WritableSignal; | ||
| import com.vaadin.signals.shared.SharedValueSignal; | ||
|
|
||
| /** | ||
| * Contains everything that Vaadin needs to store for a specific user. This is | ||
|
|
@@ -83,10 +85,14 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { | |
| final List<SessionDestroyListener> destroyListeners = new CopyOnWriteArrayList<>(); | ||
|
|
||
| /** | ||
| * Default locale of the session. | ||
| * Locale value used for serialization. The signal is the runtime source of | ||
| * truth; this field is only used to persist the value across serialization. | ||
| */ | ||
| private Locale locale = Locale.getDefault(); | ||
|
|
||
| private transient SharedValueSignal<Locale> localeSignal = new SharedValueSignal<>( | ||
| locale); | ||
|
|
||
| /** | ||
| * Session wide error handler which is used by default if an error is left | ||
| * unhandled. | ||
|
|
@@ -383,7 +389,26 @@ public DeploymentConfiguration getConfiguration() { | |
| */ | ||
| public Locale getLocale() { | ||
| checkHasLock(); | ||
| return locale; | ||
| return localeSignal.value(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here as in UI class. Change to |
||
| } | ||
|
|
||
| /** | ||
| * Gets a signal that holds the current locale of this session. | ||
| * <p> | ||
| * The signal is the source of truth for the locale. Reading the signal is | ||
| * equivalent to calling {@link #getLocale()}. | ||
| * <p> | ||
| * Note that writing directly to the signal will not propagate the locale | ||
| * change to UIs in this session. Use {@link #setLocale(Locale)} if you need | ||
| * the locale to be set on all UIs. | ||
| * | ||
| * @return a writable signal holding the current locale, never null | ||
| * @see #setLocale(Locale) | ||
| * @see #getLocale() | ||
| */ | ||
| public WritableSignal<Locale> localeSignal() { | ||
| checkHasLock(); | ||
| return localeSignal; | ||
Artur- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -399,7 +424,7 @@ public void setLocale(Locale locale) { | |
| assert locale != null : "Null locale is not supported!"; | ||
|
|
||
| checkHasLock(); | ||
| this.locale = locale; | ||
| localeSignal.value(locale); | ||
|
|
||
| getUIs().forEach(ui -> { | ||
| Map<Class<?>, CurrentInstance> oldInstances = CurrentInstance | ||
|
|
@@ -1093,6 +1118,7 @@ private void readObject(ObjectInputStream stream) | |
| uIs = (Map<Integer, UI>) stream.readObject(); | ||
| resourceRegistry = (StreamResourceRegistry) stream.readObject(); | ||
| pendingAccessQueue = new ConcurrentLinkedQueue<>(); | ||
| localeSignal = new SharedValueSignal<>(locale); | ||
| } finally { | ||
| CurrentInstance.clearAll(); | ||
| CurrentInstance.restoreInstances(old); | ||
|
|
@@ -1118,6 +1144,8 @@ private void writeObject(java.io.ObjectOutputStream stream) | |
| } | ||
| } | ||
|
|
||
| // Sync locale field from signal for serialization | ||
| locale = localeSignal.value(); | ||
| stream.defaultWriteObject(); | ||
| if (serializeUIs) { | ||
| stream.writeObject(uIs); | ||
|
|
||
126 changes: 126 additions & 0 deletions
126
flow-server/src/test/java/com/vaadin/flow/component/UILocaleSignalTest.java
This file contains hidden or 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,126 @@ | ||
| /* | ||
| * Copyright 2000-2026 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.flow.component; | ||
|
|
||
| import java.util.Locale; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import com.vaadin.flow.dom.SignalsUnitTest; | ||
| import com.vaadin.signals.WritableSignal; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertSame; | ||
|
|
||
| /** | ||
| * Unit tests for {@link UI#localeSignal()}. | ||
| */ | ||
| public class UILocaleSignalTest extends SignalsUnitTest { | ||
|
|
||
| @Test | ||
| public void localeSignal_initialValue_matchesGetLocale() { | ||
| UI ui = UI.getCurrent(); | ||
| WritableSignal<Locale> signal = ui.localeSignal(); | ||
|
|
||
| assertNotNull("localeSignal() should never return null", signal); | ||
| assertEquals("Signal value should match getLocale()", ui.getLocale(), | ||
| signal.value()); | ||
| } | ||
|
|
||
| @Test | ||
| public void localeSignal_setLocale_signalUpdated() { | ||
| UI ui = UI.getCurrent(); | ||
| WritableSignal<Locale> signal = ui.localeSignal(); | ||
|
|
||
| Locale initialLocale = ui.getLocale(); | ||
| Locale newLocale = Locale.FRENCH; | ||
|
|
||
| // Ensure we're actually changing the locale | ||
| if (initialLocale.equals(newLocale)) { | ||
| newLocale = Locale.GERMAN; | ||
| } | ||
|
|
||
| ui.setLocale(newLocale); | ||
|
|
||
| assertEquals("Signal should reflect the new locale after setLocale()", | ||
| newLocale, signal.value()); | ||
| assertEquals("getLocale() should also return the new locale", newLocale, | ||
| ui.getLocale()); | ||
| } | ||
|
|
||
| @Test | ||
| public void localeSignal_writeToSignal_updatesGetLocale() { | ||
| UI ui = UI.getCurrent(); | ||
| WritableSignal<Locale> signal = ui.localeSignal(); | ||
|
|
||
| Locale initialLocale = ui.getLocale(); | ||
| Locale newLocale = Locale.FRENCH; | ||
|
|
||
| // Ensure we're actually changing the locale | ||
| if (initialLocale.equals(newLocale)) { | ||
| newLocale = Locale.GERMAN; | ||
| } | ||
|
|
||
| signal.value(newLocale); | ||
|
|
||
| assertEquals("getLocale() should reflect the new locale after " | ||
| + "writing to signal", newLocale, ui.getLocale()); | ||
| assertEquals("Signal should have the new value", newLocale, | ||
| signal.value()); | ||
| } | ||
|
|
||
| @Test | ||
| public void localeSignal_sameInstance_returnedOnMultipleCalls() { | ||
| UI ui = UI.getCurrent(); | ||
|
|
||
| WritableSignal<Locale> signal1 = ui.localeSignal(); | ||
| WritableSignal<Locale> signal2 = ui.localeSignal(); | ||
|
|
||
| assertSame("localeSignal() should return the same instance on " | ||
| + "multiple calls", signal1, signal2); | ||
| } | ||
|
|
||
| @Test | ||
| public void localeSignal_multipleLocaleChanges_signalFollows() { | ||
| UI ui = UI.getCurrent(); | ||
| WritableSignal<Locale> signal = ui.localeSignal(); | ||
|
|
||
| ui.setLocale(Locale.FRENCH); | ||
| assertEquals(Locale.FRENCH, signal.value()); | ||
|
|
||
| ui.setLocale(Locale.GERMAN); | ||
| assertEquals(Locale.GERMAN, signal.value()); | ||
|
|
||
| ui.setLocale(Locale.JAPANESE); | ||
| assertEquals(Locale.JAPANESE, signal.value()); | ||
| } | ||
|
|
||
| @Test | ||
| public void localeSignal_multipleSignalWrites_getLocaleFollows() { | ||
| UI ui = UI.getCurrent(); | ||
| WritableSignal<Locale> signal = ui.localeSignal(); | ||
|
|
||
| signal.value(Locale.FRENCH); | ||
| assertEquals(Locale.FRENCH, ui.getLocale()); | ||
|
|
||
| signal.value(Locale.GERMAN); | ||
| assertEquals(Locale.GERMAN, ui.getLocale()); | ||
|
|
||
| signal.value(Locale.JAPANESE); | ||
| assertEquals(Locale.JAPANESE, ui.getLocale()); | ||
| } | ||
| } |
131 changes: 131 additions & 0 deletions
131
flow-server/src/test/java/com/vaadin/flow/server/VaadinSessionLocaleSignalTest.java
This file contains hidden or 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,131 @@ | ||
| /* | ||
| * Copyright 2000-2026 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.flow.server; | ||
|
|
||
| import java.util.Locale; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import com.vaadin.flow.component.UI; | ||
| import com.vaadin.flow.dom.SignalsUnitTest; | ||
| import com.vaadin.signals.WritableSignal; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertSame; | ||
|
|
||
| /** | ||
| * Unit tests for {@link VaadinSession#localeSignal()}. | ||
| */ | ||
| public class VaadinSessionLocaleSignalTest extends SignalsUnitTest { | ||
|
|
||
| private VaadinSession getSession() { | ||
| return UI.getCurrent().getSession(); | ||
| } | ||
|
|
||
| @Test | ||
| public void localeSignal_initialValue_matchesGetLocale() { | ||
| VaadinSession session = getSession(); | ||
| WritableSignal<Locale> signal = session.localeSignal(); | ||
|
|
||
| assertNotNull("localeSignal() should never return null", signal); | ||
| assertEquals("Signal value should match getLocale()", | ||
| session.getLocale(), signal.value()); | ||
| } | ||
|
|
||
| @Test | ||
| public void localeSignal_setLocale_signalUpdated() { | ||
| VaadinSession session = getSession(); | ||
| WritableSignal<Locale> signal = session.localeSignal(); | ||
|
|
||
| Locale initialLocale = session.getLocale(); | ||
| Locale newLocale = Locale.FRENCH; | ||
|
|
||
| // Ensure we're actually changing the locale | ||
| if (initialLocale.equals(newLocale)) { | ||
| newLocale = Locale.GERMAN; | ||
| } | ||
|
|
||
| session.setLocale(newLocale); | ||
|
|
||
| assertEquals("Signal should reflect the new locale after setLocale()", | ||
| newLocale, signal.value()); | ||
| assertEquals("getLocale() should also return the new locale", newLocale, | ||
| session.getLocale()); | ||
| } | ||
|
|
||
| @Test | ||
| public void localeSignal_writeToSignal_updatesGetLocale() { | ||
| VaadinSession session = getSession(); | ||
| WritableSignal<Locale> signal = session.localeSignal(); | ||
|
|
||
| Locale initialLocale = session.getLocale(); | ||
| Locale newLocale = Locale.FRENCH; | ||
|
|
||
| // Ensure we're actually changing the locale | ||
| if (initialLocale.equals(newLocale)) { | ||
| newLocale = Locale.GERMAN; | ||
| } | ||
|
|
||
| signal.value(newLocale); | ||
|
|
||
| assertEquals("getLocale() should reflect the new locale after " | ||
| + "writing to signal", newLocale, session.getLocale()); | ||
| assertEquals("Signal should have the new value", newLocale, | ||
| signal.value()); | ||
| } | ||
|
|
||
| @Test | ||
| public void localeSignal_sameInstance_returnedOnMultipleCalls() { | ||
| VaadinSession session = getSession(); | ||
|
|
||
| WritableSignal<Locale> signal1 = session.localeSignal(); | ||
| WritableSignal<Locale> signal2 = session.localeSignal(); | ||
|
|
||
| assertSame("localeSignal() should return the same instance on " | ||
| + "multiple calls", signal1, signal2); | ||
| } | ||
|
|
||
| @Test | ||
| public void localeSignal_multipleLocaleChanges_signalFollows() { | ||
| VaadinSession session = getSession(); | ||
| WritableSignal<Locale> signal = session.localeSignal(); | ||
|
|
||
| session.setLocale(Locale.FRENCH); | ||
| assertEquals(Locale.FRENCH, signal.value()); | ||
|
|
||
| session.setLocale(Locale.GERMAN); | ||
| assertEquals(Locale.GERMAN, signal.value()); | ||
|
|
||
| session.setLocale(Locale.JAPANESE); | ||
| assertEquals(Locale.JAPANESE, signal.value()); | ||
| } | ||
|
|
||
| @Test | ||
| public void localeSignal_multipleSignalWrites_getLocaleFollows() { | ||
| VaadinSession session = getSession(); | ||
| WritableSignal<Locale> signal = session.localeSignal(); | ||
|
|
||
| signal.value(Locale.FRENCH); | ||
| assertEquals(Locale.FRENCH, session.getLocale()); | ||
|
|
||
| signal.value(Locale.GERMAN); | ||
| assertEquals(Locale.GERMAN, session.getLocale()); | ||
|
|
||
| signal.value(Locale.JAPANESE); | ||
| assertEquals(Locale.JAPANESE, session.getLocale()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
localeSignal.peek()would prevent accidental dependency creation to localeSignal. I can imagine getLocale() being called many times within signal effects, internal and external.