-
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.
This is a model, an example where a separate translation service comp…
…onent is used, which is then imported into the components where translations are needed. In such an implementation, Vue components must be called as modules
- Loading branch information
1 parent
94c757b
commit d10865c
Showing
4 changed files
with
94 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,64 @@ | ||
/* global Vue */ | ||
|
||
const termCountsApp = Vue.createApp({ | ||
data () { | ||
return { | ||
languages: [] | ||
} | ||
}, | ||
mounted () { | ||
fetch('rest/v1/' + window.SKOSMOS.vocab + '/labelStatistics?lang=' + window.SKOSMOS.lang) | ||
.then(data => { | ||
return data.json() | ||
}) | ||
.then(data => { | ||
this.languages = data.languages | ||
}) | ||
}, | ||
template: ` | ||
<h3 class="fw-bold py-3">Term counts by language</h3> | ||
<table class="table" id="term-stats"> | ||
<tbody> | ||
<tr> | ||
<th class="main-table-label fw-bold">Concept language</th> | ||
<th class="main-table-label fw-bold">Preferred terms</th> | ||
<th class="main-table-label fw-bold">Alternate terms</th> | ||
<th class="main-table-label fw-bold">Hidden terms</th> | ||
import { createI18nInstance } from './translation-service.js'; | ||
|
||
(async function () { | ||
|
||
try { | ||
const i18n = await createI18nInstance(window.SKOSMOS.lang || 'fi', 'term-counts'); | ||
|
||
const termCountsApp = Vue.createApp({ | ||
data() { | ||
return { | ||
languages: [] | ||
}; | ||
}, | ||
mounted () { | ||
fetch('rest/v1/' + window.SKOSMOS.vocab + '/labelStatistics?lang=' + window.SKOSMOS.lang) | ||
.then(data => { | ||
return data.json() | ||
}) | ||
.then(data => { | ||
this.languages = data.languages | ||
}) | ||
}, | ||
template: ` | ||
<h3 class="fw-bold py-3">{{ $t('Term counts by language') }}</h3> | ||
<table class="table" id="term-stats"> | ||
<tbody> | ||
<tr> | ||
<th class="main-table-label fw-bold">{{ $t('Concept language') }}</th> | ||
<th class="main-table-label fw-bold">{{ $t('Preferred terms') }}</th> | ||
<th class="main-table-label fw-bold">{{ $t('Alternate terms') }}</th> | ||
<th class="main-table-label fw-bold">{{ $t('Hidden terms') }}</th> | ||
</tr> | ||
<template v-if="languages.length"> | ||
<term-counts :languages="languages"></term-counts> | ||
</template> | ||
<template v-else > | ||
<i class="fa-solid fa-spinner fa-spin-pulse"></i> | ||
</template> | ||
</tbody> | ||
</table> | ||
` | ||
}); | ||
|
||
termCountsApp.component('term-counts', { | ||
props: ['languages'], | ||
template: ` | ||
<tr v-for="l in languages" :key="l.literal"> | ||
<td>{{ l.literal }}</td> | ||
<td>{{ l.properties.find(a => a.property === 'skos:prefLabel').labels }}</td> | ||
<td>{{ l.properties.find(a => a.property === 'skos:altLabel').labels }}</td> | ||
<td>{{ l.properties.find(a => a.property === 'skos:hiddenLabel').labels }}</td> | ||
</tr> | ||
<template v-if="languages.length"> | ||
<term-counts :languages="languages"></term-counts> | ||
</template> | ||
<template v-else > | ||
<i class="fa-solid fa-spinner fa-spin-pulse"></i> | ||
</template> | ||
</tbody> | ||
</table> | ||
` | ||
}) | ||
` | ||
}); | ||
|
||
termCountsApp.component('term-counts', { | ||
props: ['languages'], | ||
template: ` | ||
<tr v-for="l in languages"> | ||
<td>{{ l.literal }}</td> | ||
<td>{{ l.properties.find(a => a.property === 'skos:prefLabel').labels }}</td> | ||
<td>{{ l.properties.find(a => a.property === 'skos:altLabel').labels }}</td> | ||
<td>{{ l.properties.find(a => a.property === 'skos:hiddenLabel').labels }}</td> | ||
</tr> | ||
` | ||
}) | ||
termCountsApp.use(i18n); | ||
termCountsApp.mount('#term-counts'); | ||
|
||
termCountsApp.mount('#term-counts') | ||
} catch (error) { | ||
console.error("Ongelma alustuksessa", error); | ||
} | ||
})(); |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export async function loadLocaleMessages(locale) { | ||
const messages = {}; | ||
try { | ||
const response = await fetch(`http://localhost/skosmos/resource/translations/messages.${locale}.json`); | ||
const data = await response.json(); | ||
console.log(`Haettiin data: ${locale}:`, data); | ||
messages[locale] = data; | ||
} catch (error) { | ||
console.error('käännösten lataamisessa tapahtui virhe:', error); | ||
} | ||
return messages; | ||
} | ||
|
||
export async function createI18nInstance(locale = 'en', componentName = 'Unknown Component') { | ||
const messages = await loadLocaleMessages(locale); | ||
const i18n = VueI18n.createI18n({ | ||
locale: locale, | ||
fallbackLocale: 'en', // fallback-kieli | ||
messages | ||
}); | ||
|
||
console.log('Luotiin i18n:', i18n); | ||
return i18n; | ||
} |
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