Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
Laravel, InertiaJS, make translate from Database function globally

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.

about 4 years ago · Santiago Gelvez
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda