Skip to content

Use of local messages in composable #2207

@faber-mondata

Description

@faber-mondata

Clear and concise description of the problem

In my current project, I use component specific message files in local i18n scopes which works amazing!:

// component.vue
<script setup lang="ts">
  // Imports
  import { useI18n } from "vue-i18n";
  import i18nMessages from "./component.i18n";

  // I18n
  const { t } = useI18n({ messages: i18nMessages });
  
  const projectStatus = getProjectStatus(project);

  </script>

{{ t("component.message") }} 

{{ projectStatus }}

But if I want to do the same for a composable:

// composable.ts
  // Imports
  import { useI18n } from "vue-i18n";
  import i18nMessages from "./component.i18n";

  export function getProjectStatus(project) {
      const { t } = useI18n({ messages: i18nMessages });
      
      if(project.problem)
           return t("status.problem");
      else
           return t("status.noProblem");
  }

It doesn't work, because both calls of useI18n will execute on the same component. And the result will be:

Important Component Message // From the component.i18n messages

status.problem 

Suggested solution

  1. I missed something and it's already possible => I'll improve the docu
  2. Allow stacking of useI18n by calling useI18n to create new scope on top of the "current one"

Alternative

Merge messages into the existing i18n instance:

const { t, locale, mergeLocaleMessage } = useI18n();
mergeLocaleMessage(locale.value, i18nMessages[locale.value]);

But then the composable directly affects the messages of the component, which can lead to undesired side effects, when both use the same key.

Additional context

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    🔨 p3-minor-bugPriority 3: a bug in an edge case that only affects very specific usage

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions