feat: fetch translations
This commit is contained in:
@@ -4,6 +4,7 @@ import { createApp } from 'vue'
|
|||||||
import router from './router'
|
import router from './router'
|
||||||
import App from './App.vue'
|
import App from './App.vue'
|
||||||
import { createPinia } from 'pinia'
|
import { createPinia } from 'pinia'
|
||||||
|
import translationPlugin from './translation'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
FrappeUI,
|
FrappeUI,
|
||||||
@@ -12,7 +13,6 @@ import {
|
|||||||
frappeRequest,
|
frappeRequest,
|
||||||
resourcesPlugin,
|
resourcesPlugin,
|
||||||
} from 'frappe-ui'
|
} from 'frappe-ui'
|
||||||
import translationPlugin from './translation'
|
|
||||||
|
|
||||||
// create a pinia instance
|
// create a pinia instance
|
||||||
let pinia = createPinia()
|
let pinia = createPinia()
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
|
<div class="h-screen">
|
||||||
<header
|
<header
|
||||||
class="sticky top-0 z-10 flex items-center justify-between border-b bg-white px-3 py-2.5 sm:px-5"
|
class="sticky top-0 z-10 flex items-center justify-between border-b bg-white px-3 py-2.5 sm:px-5"
|
||||||
>
|
>
|
||||||
@@ -15,7 +16,7 @@
|
|||||||
<template #prefix>
|
<template #prefix>
|
||||||
<Plus class="h-4 w-4" />
|
<Plus class="h-4 w-4" />
|
||||||
</template>
|
</template>
|
||||||
{{ __("New Course") }}
|
New Course
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
@@ -28,11 +29,10 @@
|
|||||||
:class="{ 'text-gray-900': selected }"
|
:class="{ 'text-gray-900': selected }"
|
||||||
>
|
>
|
||||||
<component v-if="tab.icon" :is="tab.icon" class="h-5" />
|
<component v-if="tab.icon" :is="tab.icon" class="h-5" />
|
||||||
{{ __(tab.label) }}
|
{{ tab.label }}
|
||||||
<Badge
|
<Badge
|
||||||
class="group-hover:bg-gray-900"
|
:class="{ 'text-gray-900 border border-gray-900': selected }"
|
||||||
:class="[selected ? 'bg-gray-900' : 'bg-gray-600']"
|
variant="subtle"
|
||||||
variant="solid"
|
|
||||||
theme="gray"
|
theme="gray"
|
||||||
size="sm"
|
size="sm"
|
||||||
>
|
>
|
||||||
@@ -42,19 +42,21 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<template #default="{ tab }">
|
<template #default="{ tab }">
|
||||||
<div class="grid grid-cols-3 gap-8 mt-5" v-if="tab.courses && tab.courses.value.length && 0">
|
<div class="grid grid-cols-3 gap-8 mt-5" v-if="tab.courses && tab.courses.value.length">
|
||||||
<router-link v-for="course in tab.courses.value" :to="{ name: 'CourseDetail', params: { course: course.name } }">
|
<router-link v-for="course in tab.courses.value" :to="{ name: 'CourseDetail', params: { course: course.name } }">
|
||||||
<CourseCard :course="course" />
|
<CourseCard :course="course" />
|
||||||
</router-link>
|
</router-link>
|
||||||
</div>
|
</div>
|
||||||
<div v-else class="grid flex-1 place-items-center text-xl font-medium text-gray-500">
|
<div v-else class="grid flex-1 place-items-center text-xl font-medium text-gray-500">
|
||||||
<div class="flex flex-col items-center justify-center mt-4">
|
<div class="flex flex-col items-center justify-center mt-4">
|
||||||
<div>{{ __('No {0} courses found').format(tab.label.toLowerCase()) }}</div>
|
<div>No {{ tab.label.toLowerCase() }} courses found</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</Tabs>
|
</Tabs>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
|
|||||||
@@ -1,22 +1,22 @@
|
|||||||
import { createResource } from 'frappe-ui'
|
import { createResource } from 'frappe-ui'
|
||||||
|
|
||||||
export default function translationPlugin(app) {
|
export default function translationPlugin(app) {
|
||||||
app.config.globalProperties.__ = translate
|
app.config.globalProperties.__ = translate
|
||||||
// fetch translations
|
console.log(window.translatedMessages)
|
||||||
|
if (!window.translatedMessages) fetchTranslations()
|
||||||
if (!window.translatedMessages)
|
|
||||||
fetchTranslations().then((translations) => {
|
|
||||||
window.translatedMessages = translations
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function translate(message) {
|
function translate(message) {
|
||||||
let lang = window.lang || 'hi'
|
let lang = window.lang || 'hi'
|
||||||
let translatedMessage = /* window.translatedMessages[message] || */ message
|
let translatedMessages = window.translatedMessages || {
|
||||||
|
'All Courses': 'सभी पाठ्यक्रम',
|
||||||
|
Live: 'लाइव',
|
||||||
|
}
|
||||||
|
let translatedMessage = translatedMessages[message] || message
|
||||||
const hasPlaceholders = /{\d+}/.test(message)
|
const hasPlaceholders = /{\d+}/.test(message)
|
||||||
|
|
||||||
console.log(translatedMessage)
|
|
||||||
console.log(hasPlaceholders)
|
console.log(hasPlaceholders)
|
||||||
if (!hasPlaceholders) {
|
if (!hasPlaceholders) {
|
||||||
|
console.log(translatedMessage)
|
||||||
return translatedMessage
|
return translatedMessage
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
@@ -24,9 +24,6 @@ async function translate(message) {
|
|||||||
return translatedMessage.replace(
|
return translatedMessage.replace(
|
||||||
/{(\d+)}/g,
|
/{(\d+)}/g,
|
||||||
function (match, number) {
|
function (match, number) {
|
||||||
console.log(match, number)
|
|
||||||
console.log(args[number])
|
|
||||||
|
|
||||||
return typeof args[number] != 'undefined'
|
return typeof args[number] != 'undefined'
|
||||||
? args[number]
|
? args[number]
|
||||||
: match
|
: match
|
||||||
@@ -36,18 +33,15 @@ async function translate(message) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchTranslations() {
|
function fetchTranslations(lang) {
|
||||||
let lang = window.lang || 'hi'
|
console.log('called')
|
||||||
let translations = await createResource({
|
createResource({
|
||||||
url: 'lms.lms.api.get_translations',
|
url: 'lms.lms.api.get_translations',
|
||||||
cache: 'translations',
|
cache: 'translations',
|
||||||
auto: true,
|
auto: true,
|
||||||
|
transform: (data) => {
|
||||||
|
console.log(data)
|
||||||
|
window.translatedMessages = data.message
|
||||||
|
},
|
||||||
})
|
})
|
||||||
let translatedMessages = {}
|
|
||||||
console.log(translations.data)
|
|
||||||
translations.forEach((translation) => {
|
|
||||||
translatedMessages[translation.source_text] =
|
|
||||||
translation.translated_text
|
|
||||||
})
|
|
||||||
window.translatedMessages = translatedMessages
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -161,12 +161,10 @@ def get_user_info(user=None):
|
|||||||
return users
|
return users
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist(allow_guest=True)
|
||||||
def get_translations():
|
def get_translations():
|
||||||
if frappe.session.user != "Guest":
|
if frappe.session.user != "Guest":
|
||||||
language = frappe.db.get_value("User", frappe.session.user, "language")
|
language = frappe.db.get_value("User", frappe.session.user, "language")
|
||||||
else:
|
else:
|
||||||
language = frappe.db.get_single_value("System Settings", "language")
|
language = frappe.db.get_single_value("System Settings", "language")
|
||||||
print("language", language)
|
|
||||||
print(get_all_translations(language))
|
|
||||||
return get_all_translations(language)
|
return get_all_translations(language)
|
||||||
|
|||||||
Reference in New Issue
Block a user