question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use i18n in ts file?

See original GitHub issue

Reporting a bug?

I use i18n in .vue file usually, that is ok. But how can I use it in other typescript file? Such as follow:

<script setup lang="ts">
import { jslook } from './util'
</script>
// util.ts
import { useI18n } from 'vue-i18n'
const { t } = useI18n()

export const jslook = () => {
    console.log(t('message.hello'))
}

I expect the terminal logs right message, but it shows me error:

SyntaxError: Must be called at the top of a `setup` function

Expected behavior

no errors and show messages

Reproduction

I describe it in the bug report

System Info

windows,chrome

Screenshot

No response

Additional context

Maybe I didn’t find it in documents, if you know, please show me, thanks!

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
pommedepaincommented, Mar 2, 2022

@cboseck in the config file where you setup your i18n config, get what createI18n returns in a variable and then export it that way :

import { createI18n } from "vue-i18n";

const i18n = createI18n({
	// Set locale
	locale: "en",

	// Set fallback locale
	fallbackLocale: "fr",

	// Must be set to 'false', to use Composition API
	legacy: false,

	// Refer a global scope Composer instance of i18n
	globalInjection: true,

	// Set locale messages
	messages,
});

export default i18n;

Then, in your ts file, import the variable from your setup file and use it this way : i18n.global.t("key.of.your.translation")

4reactions
kazuponcommented, Jan 26, 2022

From vue-i18n v9, you need to use global of I18n instance that is created createI18n API docs here: https://vue-i18n.intlify.dev/api/general.html#global

In about example, I think lazy-loading example will be helped for you: https://github.com/intlify/vue-i18n-next/blob/master/examples/lazy-loading/vite/src/i18n.ts

Read more comments on GitHub >

github_iconTop Results From Across the Web

Roll Your Own JavaScript i18n Library with TypeScript – Part 1
What would it be like to build a JavaScript i18n library from scratch? And on your own? ... We'll use TypeScript to build...
Read more >
TypeScript Support - Vue I18n - Intlify
VueI18n use the resources, which locale messages, datetime formats and number formats. Especially, locale messages can be externalized as ...
Read more >
Internationalization (i18n) - ts - COOKBOOK - Angular
The Angular i18n attribute is a marker for translatable content. Place it on every element tag whose fixed text should be translated. i18n...
Read more >
How to use vue-i18n in a ts file? - Stack Overflow
useI18n relies on the Vue apparatus. I think your best bet is to pass in the Composer instance in your defaultErrorHandler function.
Read more >
Manage Vue i18n with Typescript - Medium
Using vue-i18n in a Typescript model file requires a single line of setup in main.ts. At the end of the file, you just...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found