-
Notifications
You must be signed in to change notification settings - Fork 1
/
LanguageSelect.java
88 lines (78 loc) · 3.94 KB
/
LanguageSelect.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Copyright 2019 Kaspar Scherrer
*
* 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 ch.carnet.kasparscherrer;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.select.Select;
import com.vaadin.flow.data.renderer.ComponentRenderer;
import com.vaadin.flow.server.VaadinService;
import javax.servlet.http.Cookie;
import java.util.Locale;
/**
* LANGUAGE FLAGS:
* You need to provide a flag image for each Locale/Language that you will add as item.
* Place these images in 'img/languageflags/' folder, and name them [language_code].png
* - for example en.png for english flag.
* To find out where the 'img' folder should be, see https://stackoverflow.com/questions/57553973/where-should-i-place-my-vaadin-10-static-files
* It will either be '/src/main/webapp/img' or '/src/main/resources/META-INF/resources/img'
*
* TRANSLATIONS OF LANGUAGE NAMES:
* In the ResourceBundle that your I18NProvider implementation uses, add Translations for each Locale/Language that you will use.
* The keys must be named 'LanguageSelect.[language_code]'
* - for example LanguageSelect.en=English in the English resourcebundle-file, and LanguageSelect.en=Inglés in the Spanish resourcebundle-file
*/
@CssImport(value = "./styles/sidebarSelect.css", themeFor = "vaadin-select")
public class LanguageSelect extends Select<Locale> {
public static final String LANGUAGE_COOKIE_NAME = "PreferredLanguage";
private ComponentRenderer<HorizontalLayout, Locale> languageRenderer = new ComponentRenderer<>(item -> {
HorizontalLayout hLayout = new HorizontalLayout();
Image languageFlag = new Image("img/languageflags/"+item.getLanguage()+".png", "flag for "+item.getLanguage());
languageFlag.setHeight("30px");
hLayout.add(languageFlag);
hLayout.add(new Span(getTranslation("LanguageSelect."+item.getLanguage())));
hLayout.setDefaultVerticalComponentAlignment(FlexComponent.Alignment.CENTER);
return hLayout;
});
public LanguageSelect(Locale... items){
super(items);
setEmptySelectionAllowed(false);
setRenderer(this.languageRenderer);
setValue(UI.getCurrent().getLocale());
// important that valuechangeListener is defined after setValue
addValueChangeListener(change -> {
// changes locale of current session
UI.getCurrent().getSession().setLocale(change.getValue());
// sets a cookie that will make any subsequent visit to this page use this selected language
Cookie languageCookie = new Cookie(LANGUAGE_COOKIE_NAME, change.getValue().getLanguage());
languageCookie.setMaxAge(60 * 60 * 24 * 7 * 52); // save language preference ~1 year
languageCookie.setPath("/");
VaadinService.getCurrentResponse().addCookie(languageCookie);
});
}
/**
* Call this function in localeChange() of the View.
* This will refresh all items and use the names of each language, translated in the new Locale
*/
public void refresh(){
setRenderer(this.languageRenderer);
// TODO: as soon as Select::refreshItems is public, use that!
// see https://github.com/vaadin/flow/issues/6337
}
}