How to use i18n in ts file?
See original GitHub issueReporting 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
- Read the Contributing Guidelines
- Read the Documentation
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@cboseck in the config file where you setup your i18n config, get what createI18n returns in a variable and then export it that way :
Then, in your ts file, import the variable from your setup file and use it this way :
i18n.global.t("key.of.your.translation")
From vue-i18n v9, you need to use
global
ofI18n
instance that is createdcreateI18n
API docs here: https://vue-i18n.intlify.dev/api/general.html#globalIn 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