You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript中多语言JSON字典与.d.ts类型定义关联求助

How to Connect Multilingual JSON Dictionaries with Your TypeScript dictionaries.d.ts Definition

Problem Context

I have two JSON dictionary files for different languages with the same structure, like this example:

{
"Account" : {
"AccountLockText" : "Your account is locked",
"Login" : {
"CreateAccount" : "Create an account",
"LoginBtn" : "Login",
"LoginHeader" : "Login"
}
}
}

I also have a dictionaries.d.ts file with this content:

declare module "dictionaries" {
  var Account: any;
}

I want to use these dictionaries in my TypeScript app but don't know how to link them properly. Can someone help?


Step-by-Step Solution

1. Replace any with Strongly-Typed Interfaces

First, update your dictionaries.d.ts to define exact types for your dictionary structure—this replaces the vague any with proper type safety:

declare module "dictionaries" {
  // Define nested translation types first
  interface LoginTranslations {
    CreateAccount: string;
    LoginBtn: string;
    LoginHeader: string;
  }

  // Define top-level account translations
  interface AccountTranslations {
    AccountLockText: string;
    Login: LoginTranslations;
  }

  // Export the type so you can reference it in your app
  export type { AccountTranslations };
}

2. Load JSON Files with Type Assertion

Next, import your JSON dictionaries in your TypeScript code and assert they match the type you defined. First, make sure your tsconfig.json has these settings (to enable JSON imports):

{
  "compilerOptions": {
    "resolveJsonModule": true,
    "esModuleInterop": true
  }
}

Then import and type-check your dictionaries:

// Import your language files (adjust paths to match your project)
import englishDict from './dictionaries/en.json';
import spanishDict from './dictionaries/es.json';

// Assert the JSON matches our defined type
const enTranslations = englishDict as import('dictionaries').AccountTranslations;
const esTranslations = spanishDict as import('dictionaries').AccountTranslations;

// Now you get full TypeScript intellisense!
console.log(enTranslations.AccountLockText);
console.log(esTranslations.Login.CreateAccount);

3. Add a Language Switcher (Optional)

For cleaner language switching, wrap your dictionaries in a utility function that maintains type safety:

import { AccountTranslations } from 'dictionaries';
import en from './dictionaries/en.json';
import es from './dictionaries/es.json';

// Map language codes to their dictionaries
const languageMap: Record<string, AccountTranslations> = {
  en: en as AccountTranslations,
  es: es as AccountTranslations
};

// Get the correct dictionary for a given language
export function getTranslations(lang: string): AccountTranslations {
  // Fallback to English if the requested language doesn't exist
  return languageMap[lang] || languageMap.en;
}

// Usage example
const currentTranslations = getTranslations('es');
console.log(currentTranslations.Login.LoginBtn); // Outputs Spanish login button text

4. Validate Type Safety

Now when you work with your dictionaries, TypeScript will:

  • Show autocomplete for all valid keys
  • Throw errors if you misspell a key (e.g., AccountLockTex instead of AccountLockText)
  • Ensure you don't access non-existent properties

内容的提问来源于stack exchange,提问作者Gonçalo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:11:21