I have made this helper function to translate proper column from database (MySQL). Database is like this.
Id name_en name_fr name_de
1 Car Auto Auto
2 Plane Avion Ebene
My helper function is like this..
import { Inertia } from "@inertiajs/inertia";
export default function translate(array, column) {
let locale = Inertia.page.props.locale;
let value = array[column + '_' + locale];
return value ?? 'Missing Translation';
}
In my component i call this function like this and it works fine.
<script setup>
import translate from "@/Helpers/translateFromDb";
defineProps({
object: {
type: Object,
},
});
</script>
<template>
<ul class="flex justify-start space-x-6">
<li v-for="item in object" :key="item.id">
{{ translate(item, "name") }}
</li>
</ul>
</template>
<style scoped>
</style>
Is there a way to import this helper function globally instead to call it on every component?
EDIT: Yeah i know it the same result could be obtained by using a Trait in the ObjectResource, but i'm learning Vue and i was looking to do this with it.