create translation handler

This commit is contained in:
Geriano
2022-08-14 19:37:07 +07:00
parent 6db7e40180
commit 9966ae7b38
3 changed files with 87 additions and 0 deletions

View File

@@ -34,8 +34,31 @@ const can = (abilities) => {
}
}
window.translations = {}
window.locale = localStorage.getItem('locale') || 'id'
const translation = () => axios.get(route('api.translation.get', window.locale))
.then(response => response.data)
.then(response => window.translations = response)
translation()
window.can = can
window.__ = (text, replacements = {}) => {
if (typeof text === 'string') {
if (window.translations.hasOwnProperty(text)) {
text = window.translations[text]
} else {
axios.post(route(
'api.translation.register', window.locale
), { text }).then(translation)
}
for (const key in replacements) {
text = text.replace(new RegExp(`:${key}`, 'g'), replacements[key])
}
}
return text
}