Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit e24bfb4

Browse files
committed
Create a wrapper component to set the text direction for i18n
If direction is passed in as a prop, this is used, otherwise the component will check the store for the chosen language config and use the dir property or default to 'ltr'; Add methods / properties to the store with actions and mutators. When the language picker is created, update the store with the language config matching the one chosen by the user / cookie.
1 parent 4c0dff4 commit e24bfb4

File tree

5 files changed

+78
-25
lines changed

5 files changed

+78
-25
lines changed

src/App.vue

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<template>
22
<div id="app">
3-
<cd-header></cd-header>
4-
<div class="cd-menu__content-container">
5-
<router-view></router-view>
6-
<cd-cookie-notice></cd-cookie-notice>
7-
</div>
8-
<cd-footer></cd-footer>
3+
<cd-text-direction-wrapper>
4+
<cd-header></cd-header>
5+
<div class="cd-menu__content-container">
6+
<router-view></router-view>
7+
<cd-cookie-notice></cd-cookie-notice>
8+
</div>
9+
<cd-footer></cd-footer>
10+
</cd-text-direction-wrapper>
911
</div>
1012
</template>
1113

@@ -14,11 +16,13 @@
1416
import cdHeader from './common/cd-header';
1517
import cdFooter from './common/cd-footer';
1618
import cdCookieNotice from './common/cd-cookie-notice';
19+
import cdTextDirectionWrapper from './common/cd-text-direction-wrapper';
1720
1821
export default {
1922
name: 'app',
2023
store,
2124
components: {
25+
cdTextDirectionWrapper,
2226
cdHeader,
2327
cdFooter,
2428
cdCookieNotice,

src/common/cd-covid-banner.vue

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
<template>
22
<div class="cd-banner">
3-
<p>
4-
{{ $t('Thank you for your interest in joining the CoderDojo community!') }}
5-
{{ $t('Due to the coronavirus pandemic, there are different restrictions in place across the world.') }}
6-
{{ $t('Please follow the appropriate public health advice for your country or region before planning in-person club events.') }}
7-
</p>
8-
<p>
9-
{{ $t('If you are ready to hold in-person club events soon, please') }}
10-
<a class="cd-banner-link" href="https://zen.coderdojo.com/dashboard/start-dojo">{{ $t('register your details') }}</a>
11-
{{ $t('and we will be in touch.') }}
12-
</p>
13-
<p>
14-
{{ $t('If you’re not able to start your Dojo in person yet, there are many other ways that you can get involved in the meantime.') }}
15-
{{ $t('Check out the exciting opportunities for young people, parents, volunteers, and educators to learn and get creative with tech through') }}
16-
<a class="cd-banner-link" target="_blank" href="https://www.raspberrypi.org/learn/?utm_source=coderdojo&amp;utm_medium=covid-banner&amp;utm_campaign=dmah">{{ $t('Digital Making at Home') }}</a>
17-
{{ $t('from the Raspberry Pi Foundation.') }}
18-
</p>
3+
<cd-text-direction-wrapper direction="ltr">
4+
<p>
5+
{{ $t('Thank you for your interest in joining the CoderDojo community!') }}
6+
{{ $t('Due to the coronavirus pandemic, there are different restrictions in place across the world.') }}
7+
{{ $t('Please follow the appropriate public health advice for your country or region before planning in-person club events.') }}
8+
</p>
9+
<p>
10+
{{ $t('If you are ready to hold in-person club events soon, please') }}
11+
<a class="cd-banner-link" href="https://zen.coderdojo.com/dashboard/start-dojo">{{ $t('register your details') }}</a>
12+
{{ $t('and we will be in touch.') }}
13+
</p>
14+
<p>
15+
{{ $t('If you’re not able to start your Dojo in person yet, there are many other ways that you can get involved in the meantime.') }}
16+
{{ $t('Check out the exciting opportunities for young people, parents, volunteers, and educators to learn and get creative with tech through') }}
17+
<a class="cd-banner-link" target="_blank" href="https://www.raspberrypi.org/learn/?utm_source=coderdojo&amp;utm_medium=covid-banner&amp;utm_campaign=dmah">{{ $t('Digital Making at Home') }}</a>
18+
{{ $t('from the Raspberry Pi Foundation.') }}
19+
</p>
20+
</cd-text-direction-wrapper>
1921
</div>
2022
</template>
2123

2224
<script>
25+
import cdTextDirectionWrapper from './cd-text-direction-wrapper';
26+
27+
export default {
28+
components: {
29+
cdTextDirectionWrapper,
30+
},
31+
};
2332
</script>
2433

2534
<style scoped lang="less">
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<template>
2+
<div class="lang-wrapper" v-bind:dir="textDirection">
3+
<slot></slot>
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
props: ['direction'],
10+
computed: {
11+
textDirection() {
12+
if (this.direction) {
13+
return this.direction;
14+
}
15+
16+
const config = this.$store.getters.chosenLanguageConfig;
17+
let direction = 'ltr';
18+
if (config && config.dir) {
19+
direction = config.dir;
20+
}
21+
return direction;
22+
},
23+
},
24+
};
25+
</script>
26+
27+
<style>
28+
</style>

src/locale/cd-lang-picker.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div class="col-md-4 col-sm-6">
44
<span class="fa fa-language fa-2x cd-lang-picker__icon"></span>
55
<select class="cd-lang-picker__select" v-model="lang">
6-
<option v-for="availableLanguage in availableLanguages" :value="availableLanguage.code">{{ availableLanguage.name }} ({{ availableLanguage.country }})</option>
6+
<option v-for="availableLanguage in availableLanguages" :value="availableLanguage.code" v-bind:key="availableLanguage.code">{{ availableLanguage.name }} ({{ availableLanguage.country }})</option>
77
</select>
88
</div>
99
<div class="col-md-8 col-sm-6">
@@ -52,22 +52,26 @@
5252
this.setMomentLocale(val);
5353
this.$i18n.setLocaleMessage(val, strings);
5454
this.$i18n.locale = val;
55+
const matchingLanguageConfig = find(this.availableLanguages,
56+
language => language.code === val);
57+
this.$store.dispatch('updateChosenLanguageConfig', matchingLanguageConfig);
5558
});
5659
},
5760
},
5861
async created() {
5962
this.availableLanguages = (await this.getAvailableLanguages()).body;
6063
const browserLocale = navigator.language.replace('-', '_');
61-
const matchingLocale = find(this.availableLanguages,
64+
const matchingLanguageConfig = find(this.availableLanguages,
6265
language => language.code === browserLocale);
6366
const langCookie = Cookie.get('NG_TRANSLATE_LANG_KEY');
6467
if (langCookie) {
6568
this.lang = langCookie.substring(1, langCookie.length - 1);
66-
} else if (matchingLocale) {
69+
} else if (matchingLanguageConfig) {
6770
this.lang = browserLocale;
6871
} else {
6972
this.lang = 'en_US';
7073
}
74+
this.$store.dispatch('updateChosenLanguageConfig', matchingLanguageConfig);
7175
},
7276
};
7377
</script>

src/store/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default new Vuex.Store({
88
state: {
99
loggedInUser: null,
1010
dojo: null,
11+
chosenLanguageConfig: null,
1112
},
1213
mutations: {
1314
setLoggedInUser(state, user) {
@@ -16,6 +17,9 @@ export default new Vuex.Store({
1617
setDojo(state, dojo) {
1718
Vue.set(state, 'dojo', dojo);
1819
},
20+
setChosenLanguageConfig(state, chosenLanguageConfig) {
21+
Vue.set(state, 'chosenLanguageConfig', chosenLanguageConfig);
22+
},
1923
},
2024
actions: {
2125
async getLoggedInUser({ commit }) {
@@ -28,6 +32,9 @@ export default new Vuex.Store({
2832
const dojo = (await Vue.http.get(`${Vue.config.apiServer}/api/2.0/dojos/${dojoId}`)).body;
2933
commit('setDojo', dojo);
3034
},
35+
updateChosenLanguageConfig({ commit }, chosenLanguageConfig) {
36+
commit('setChosenLanguageConfig', chosenLanguageConfig);
37+
},
3138
},
3239
getters: {
3340
loggedInUser: (state) => {
@@ -45,6 +52,7 @@ export default new Vuex.Store({
4552
state.loggedInUser.login !== null &&
4653
state.loggedInUser.login.id !== undefined,
4754
dojo: state => state.dojo,
55+
chosenLanguageConfig: state => state.chosenLanguageConfig,
4856
// User properties
4957
hasRequests: (state, getters) => getters.isLoggedIn &&
5058
getters.loggedInUser.joinRequests &&

0 commit comments

Comments
 (0)