-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The first version of translation functionality in Twig templates, whe…
…re translations are visible in the browser and the translation is read at the correct point in the code
- Loading branch information
1 parent
5e4ea21
commit 2fa7f87
Showing
3 changed files
with
90 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,65 +1,94 @@ | ||
/* global Vue */ | ||
async function loadLocaleMessages() { | ||
const locales = ['en', 'fi']; | ||
const messages = {}; | ||
|
||
const resourceCountsApp = Vue.createApp({ | ||
data () { | ||
return { | ||
concepts: {}, | ||
subTypes: {}, | ||
conceptGroups: {} | ||
} | ||
}, | ||
computed: { | ||
hasCounts () { | ||
return Object.keys(this.concepts).length > 0 | ||
} | ||
}, | ||
mounted () { | ||
fetch('rest/v1/' + window.SKOSMOS.vocab + '/vocabularyStatistics?lang=' + window.SKOSMOS.lang) | ||
.then(data => { | ||
return data.json() | ||
}) | ||
.then(data => { | ||
this.concepts = data.concepts | ||
this.subTypes = data.subTypes | ||
this.conceptGroups = data.conceptGroups | ||
}) | ||
}, | ||
template: ` | ||
<h3 class="fw-bold py-3">Resource counts by type</h3> | ||
<table class="table" id="resource-stats"> | ||
<tbody> | ||
<tr><th class="versal">Type</th><th class="versal">Count</th></tr> | ||
for (const locale of locales) { | ||
const response = await fetch(`http://localhost/skosmos/resource/translations/messages.${locale}.json`); | ||
const data = await response.json(); | ||
console.log(data); | ||
messages[locale] = data; | ||
} | ||
|
||
return messages; | ||
} | ||
|
||
async function createI18nInstance() { | ||
const messages = await loadLocaleMessages(); | ||
|
||
const i18n = VueI18n.createI18n({ | ||
locale: window.SKOSMOS.lang || 'en', // oletuskieli | ||
fallbackLocale: 'en', // Fallback | ||
messages, | ||
}); | ||
|
||
return i18n; | ||
} | ||
|
||
(async function() { | ||
const i18n = await createI18nInstance(); | ||
|
||
const resourceCountsApp = Vue.createApp({ | ||
data () { | ||
return { | ||
concepts: {}, | ||
subTypes: {}, | ||
conceptGroups: {} | ||
} | ||
}, | ||
computed: { | ||
hasCounts () { | ||
return Object.keys(this.concepts).length > 0 | ||
} | ||
}, | ||
mounted () { | ||
fetch('rest/v1/' + window.SKOSMOS.vocab + '/vocabularyStatistics?lang=' + window.SKOSMOS.lang) | ||
.then(data => { | ||
return data.json() | ||
}) | ||
.then(data => { | ||
this.concepts = data.concepts | ||
this.subTypes = data.subTypes | ||
this.conceptGroups = data.conceptGroups | ||
}) | ||
}, | ||
template: ` | ||
<h3 class="fw-bold py-3">{{ $t('Resource counts by type') }}</h3> | ||
<table class="table" id="resource-stats"> | ||
<tbody> | ||
<tr><th class="versal">{{ $t('Type') }}</th><th class="versal">{{ $t('Count') }}</th></tr> | ||
<template v-if="hasCounts"> | ||
<resource-counts :concepts="concepts" :subTypes="subTypes" :conceptGroups="conceptGroups"></resource-counts> | ||
</template> | ||
<template v-else > | ||
<i class="fa-solid fa-spinner fa-spin-pulse"></i> | ||
</template> | ||
</tbody> | ||
</table> | ||
` | ||
}) | ||
</tbody> | ||
</table> | ||
` | ||
}) | ||
|
||
resourceCountsApp.component('resource-counts', { | ||
props: ['concepts', 'subTypes', 'conceptGroups'], | ||
template: ` | ||
<tr> | ||
<td>{{ concepts.label }}</td> | ||
<td>{{ concepts.count }}</td> | ||
</tr> | ||
<tr v-for="st in subTypes"> | ||
<td>* {{ st.label }}</td> | ||
<td>{{ st.count }}</td> | ||
</tr> | ||
<tr> | ||
<td>* Käytöstä poistettu käsite</td> | ||
<td>{{ concepts.deprecatedCount }}</td> | ||
</tr> | ||
<tr v-if="conceptGroups"> | ||
<td>{{ conceptGroups.label }}</td> | ||
<td>{{ conceptGroups.count }}</td> | ||
</tr> | ||
` | ||
}) | ||
resourceCountsApp.component('resource-counts', { | ||
props: ['concepts', 'subTypes', 'conceptGroups'], | ||
template: ` | ||
<tr> | ||
<td>{{ concepts.label }}</td> | ||
<td>{{ concepts.count }}</td> | ||
</tr> | ||
<tr v-for="st in subTypes"> | ||
<td>* {{ st.label }}</td> | ||
<td>{{ st.count }}</td> | ||
</tr> | ||
<tr> | ||
<td>* Käytöstä poistettu käsite</td> | ||
<td>{{ concepts.deprecatedCount }}</td> | ||
</tr> | ||
<tr v-if="conceptGroups"> | ||
<td>{{ conceptGroups.label }}</td> | ||
<td>{{ conceptGroups.count }}</td> | ||
</tr> | ||
` | ||
}) | ||
|
||
resourceCountsApp.mount('#resource-counts') | ||
resourceCountsApp.use(i18n); | ||
resourceCountsApp.mount('#resource-counts'); | ||
})(); |
This file contains 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