Skip to content

Commit

Permalink
Add change language option. (kubernetes#5693)
Browse files Browse the repository at this point in the history
* Add change language option. Move available languages to index.config. Make lang value in cookie the most important, then the env variable and the least important is the default browser header.

* Update zh translations based on helight suggestions.

* Refactor code and fix caching issue

* Small optimizations

Co-authored-by: Sebastian Florek <sebastian.florek@kubermatic.com>
  • Loading branch information
theSinner and Sebastian Florek authored Nov 26, 2020
1 parent cb66bd2 commit 72c275c
Show file tree
Hide file tree
Showing 21 changed files with 279 additions and 54 deletions.
11 changes: 11 additions & 0 deletions aio/scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ function build::frontend {
--prod \
--localize \
--outputPath=${FRONTEND_DIR}

# Avoid locale caching due to the same output file naming
# We'll add language code prefix to the generated main javascript file.
languages=($(ls ${FRONTEND_DIR}))
for language in "${languages[@]}"; do
localeDir=${FRONTEND_DIR}/${language}
filename=("$(find "${localeDir}" -name 'main.*.js' -exec basename {} \;)")

mv "${localeDir}/${filename}" "${localeDir}/${language}.${filename}"
sed -i "s/${filename}/${language}.${filename}/" "${localeDir}/index.html"
done
}

function build::backend {
Expand Down
1 change: 1 addition & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"extractCss": true,
"namedChunks": false,
"aot": true,
"localize": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
Expand Down
16 changes: 16 additions & 0 deletions i18n/de/messages.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -5442,6 +5442,22 @@
<context context-type="linenumber">27</context>
</context-group>
</trans-unit>
<trans-unit id="fe46ccaae902ce974e2441abe752399288298619" datatype="html">
<source>Language</source>
<target state="new">Sprache</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">37</context>
</context-group>
</trans-unit>
<trans-unit id="170c19b70d40071916e39cad610ac12f269dd473" datatype="html">
<source>Change the language of the dashboard</source>
<target state="new">Ändern Sie die Sprache des Dashboards</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">39</context>
</context-group>
</trans-unit>
</body>
</file>
</xliff>
16 changes: 16 additions & 0 deletions i18n/fr/messages.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -5456,6 +5456,22 @@
<context context-type="linenumber">27</context>
</context-group>
</trans-unit>
<trans-unit id="fe46ccaae902ce974e2441abe752399288298619" datatype="html">
<source>Language</source>
<target state="new">Langue</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">37</context>
</context-group>
</trans-unit>
<trans-unit id="170c19b70d40071916e39cad610ac12f269dd473" datatype="html">
<source>Change the language of the dashboard</source>
<target state="new">Changer la langue du tableau de bord</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">39</context>
</context-group>
</trans-unit>
</body>
</file>
</xliff>
16 changes: 16 additions & 0 deletions i18n/ja/messages.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -5402,6 +5402,22 @@
<context context-type="linenumber">27</context>
</context-group>
</trans-unit>
<trans-unit id="fe46ccaae902ce974e2441abe752399288298619" datatype="html">
<source>Language</source>
<target state="new">言語</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">37</context>
</context-group>
</trans-unit>
<trans-unit id="170c19b70d40071916e39cad610ac12f269dd473" datatype="html">
<source>Change the language of the dashboard</source>
<target state="new">ダッシュボードの言語を変更する</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">39</context>
</context-group>
</trans-unit>
<trans-unit id="dc935c1eb87651baf1b1b0e53119e6dec2db8862" datatype="html">
<source>
Shell in
Expand Down
16 changes: 16 additions & 0 deletions i18n/ko/messages.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -5514,6 +5514,22 @@
<context context-type="linenumber">27</context>
</context-group>
</trans-unit>
<trans-unit id="fe46ccaae902ce974e2441abe752399288298619" datatype="html">
<source>Language</source>
<target state="new">언어</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">37</context>
</context-group>
</trans-unit>
<trans-unit id="170c19b70d40071916e39cad610ac12f269dd473" datatype="html">
<source>Change the language of the dashboard</source>
<target state="new">대시 보드의 언어 변경</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">39</context>
</context-group>
</trans-unit>
<trans-unit id="dc935c1eb87651baf1b1b0e53119e6dec2db8862" datatype="html">
<source>
Shell in
Expand Down
14 changes: 14 additions & 0 deletions i18n/messages.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -3748,6 +3748,20 @@
<context context-type="linenumber">27</context>
</context-group>
</trans-unit>
<trans-unit id="fe46ccaae902ce974e2441abe752399288298619" datatype="html">
<source>Language</source>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">37</context>
</context-group>
</trans-unit>
<trans-unit id="170c19b70d40071916e39cad610ac12f269dd473" datatype="html">
<source>Change the language of the dashboard</source>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">39</context>
</context-group>
</trans-unit>
<trans-unit id="004b222ff9ef9dd4771b777950ca1d0e4cd4348a" datatype="html">
<source>About</source>
<context-group purpose="location">
Expand Down
16 changes: 16 additions & 0 deletions i18n/zh-Hans/messages.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -5517,6 +5517,22 @@
<context context-type="linenumber">27</context>
</context-group>
</trans-unit>
<trans-unit id="fe46ccaae902ce974e2441abe752399288298619" datatype="html">
<source>Language</source>
<target state="new">语言</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">37</context>
</context-group>
</trans-unit>
<trans-unit id="170c19b70d40071916e39cad610ac12f269dd473" datatype="html">
<source>Change the language of the dashboard</source>
<target state="new">更改 dashboard 的语言</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">39</context>
</context-group>
</trans-unit>
<trans-unit id="dc935c1eb87651baf1b1b0e53119e6dec2db8862" datatype="html">
<source>
Shell in
Expand Down
16 changes: 16 additions & 0 deletions i18n/zh-Hant-HK/messages.zh-Hant-HK.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -5517,6 +5517,22 @@
<context context-type="linenumber">27</context>
</context-group>
</trans-unit>
<trans-unit id="fe46ccaae902ce974e2441abe752399288298619" datatype="html">
<source>Language</source>
<target state="new">语言</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">37</context>
</context-group>
</trans-unit>
<trans-unit id="170c19b70d40071916e39cad610ac12f269dd473" datatype="html">
<source>Change the language of the dashboard</source>
<target state="new">更改 dashboard 的语言</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">39</context>
</context-group>
</trans-unit>
<trans-unit id="dc935c1eb87651baf1b1b0e53119e6dec2db8862" datatype="html">
<source>
Shell in
Expand Down
16 changes: 16 additions & 0 deletions i18n/zh-Hant/messages.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -5517,6 +5517,22 @@
<context context-type="linenumber">27</context>
</context-group>
</trans-unit>
<trans-unit id="fe46ccaae902ce974e2441abe752399288298619" datatype="html">
<source>Language</source>
<target state="new">語言</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">37</context>
</context-group>
</trans-unit>
<trans-unit id="170c19b70d40071916e39cad610ac12f269dd473" datatype="html">
<source>Change the language of the dashboard</source>
<target state="new">更改 dashboard 的語言</target>
<context-group purpose="location">
<context context-type="sourcefile">../src/app/frontend/settings/local/template.html</context>
<context context-type="linenumber">39</context>
</context-group>
</trans-unit>
<trans-unit id="dc935c1eb87651baf1b1b0e53119e6dec2db8862" datatype="html">
<source>
Shell in
Expand Down
16 changes: 10 additions & 6 deletions src/app/backend/handler/localehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,17 @@ func getAssetsDir() string {

// LocaleHandler serves different html versions based on the Accept-Language header.
func (handler *LocaleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.EscapedPath() == "/" || r.URL.EscapedPath() == "/index.html" {
// Do not store the html page in the cache. If the user is to click on 'switch language',
// we want a different index.html (for the right locale) to be served when the page refreshes.
w.Header().Add("Cache-Control", "no-store")
acceptLanguage := ""
cookie, err := r.Cookie("lang")
if err == nil {
acceptLanguage = cookie.Value
}
acceptLanguage := os.Getenv("ACCEPT_LANGUAGE")
if acceptLanguage == "" {

if len(acceptLanguage) == 0 {
acceptLanguage = os.Getenv("ACCEPT_LANGUAGE")
}

if len(acceptLanguage) == 0 {
acceptLanguage = r.Header.Get("Accept-Language")
}

Expand Down
26 changes: 13 additions & 13 deletions src/app/frontend/common/components/namespace/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {Component, ElementRef, Inject, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {MatDialog} from '@angular/material/dialog';
import {MatSelect} from '@angular/material/select';
import {ActivatedRoute, NavigationEnd, Router} from '@angular/router';
import {NamespaceList} from '@api/backendapi';
import {Subject} from 'rxjs';
import {distinctUntilChanged, filter, startWith, switchMap, takeUntil} from 'rxjs/operators';

import {CONFIG} from '../../../index.config';
import {Config, CONFIG_DI_TOKEN} from '../../../index.config';
import {NAMESPACE_STATE_PARAM} from '../../params/params';
import {HistoryService} from '../../services/global/history';
import {NamespaceService} from '../../services/global/namespace';
import {NotificationSeverity, NotificationsService} from '../../services/global/notifications';
import {NotificationsService} from '../../services/global/notifications';
import {KdStateService} from '../../services/global/state';
import {EndpointManager, Resource} from '../../services/resource/endpoint';
import {ResourceService} from '../../services/resource/resource';

import {NamespaceChangeDialog} from './changedialog/dialog';

@Component({
Expand Down Expand Up @@ -57,12 +56,13 @@ export class NamespaceSelectorComponent implements OnInit, OnDestroy {
private readonly dialog_: MatDialog,
private readonly kdState_: KdStateService,
private readonly notifications_: NotificationsService,
private readonly _activatedRoute: ActivatedRoute,
private readonly _historyService: HistoryService
private readonly activatedRoute_: ActivatedRoute,
private readonly historyService_: HistoryService,
@Inject(CONFIG_DI_TOKEN) private readonly appConfig_: Config
) {}

ngOnInit(): void {
this._activatedRoute.queryParams.pipe(takeUntil(this.unsubscribe_)).subscribe(params => {
this.activatedRoute_.queryParams.pipe(takeUntil(this.unsubscribe_)).subscribe(params => {
const namespace = params.namespace;
if (!namespace) {
this.setDefaultQueryParams_();
Expand Down Expand Up @@ -183,12 +183,12 @@ export class NamespaceSelectorComponent implements OnInit, OnDestroy {
if (confirmed) {
this.selectedNamespace = this._getCurrentResourceNamespaceParam();
this.router_.navigate([], {
relativeTo: this._activatedRoute,
relativeTo: this.activatedRoute_,
queryParams: {[NAMESPACE_STATE_PARAM]: this.selectedNamespace},
queryParamsHandling: 'merge',
});
} else {
this._historyService.goToPreviousState('overview');
this.historyService_.goToPreviousState('overview');
}
});
}
Expand All @@ -205,7 +205,7 @@ export class NamespaceSelectorComponent implements OnInit, OnDestroy {
} else {
// Change only the namespace as currently not on details view.
this.router_.navigate([], {
relativeTo: this._activatedRoute,
relativeTo: this.activatedRoute_,
queryParams: {[NAMESPACE_STATE_PARAM]: namespace},
queryParamsHandling: 'merge',
});
Expand All @@ -229,7 +229,7 @@ export class NamespaceSelectorComponent implements OnInit, OnDestroy {
}

private _getCurrentRoute(): ActivatedRoute {
let route = this._activatedRoute.root;
let route = this.activatedRoute_.root;
while (route && route.firstChild) {
route = route.firstChild;
}
Expand All @@ -247,8 +247,8 @@ export class NamespaceSelectorComponent implements OnInit, OnDestroy {
}

setDefaultQueryParams_() {
this.router_.navigate([this._activatedRoute.snapshot.url], {
queryParams: {[NAMESPACE_STATE_PARAM]: CONFIG.defaultNamespace},
this.router_.navigate([this.activatedRoute_.snapshot.url], {
queryParams: {[NAMESPACE_STATE_PARAM]: this.appConfig_.defaultNamespace},
queryParamsHandling: 'merge',
});
}
Expand Down
Loading

0 comments on commit 72c275c

Please sign in to comment.