Skip to content

Instantly share code, notes, and snippets.

@rndmcnlly
Last active February 15, 2024 23:10
Show Gist options
  • Save rndmcnlly/4bb46bd1f8a63f45c8dc5ea24a419f5b to your computer and use it in GitHub Desktop.
Save rndmcnlly/4bb46bd1f8a63f45c8dc5ea24a419f5b to your computer and use it in GitHub Desktop.

Prompt:

I want to demonstrate the idea of localization support in a game. Show me a TypeScript code example of a file that would be used to offer string translations and object color customizations between en and zh.

Response:

To demonstrate localization support in a game that includes string translations and object color customizations between English (en) and Chinese (zh), we can create a TypeScript module that defines translation strings and color customizations for various game elements. We'll use a simple approach where we define a type for our localization data and then provide specific implementations for each language, including text and color customizations for game elements like buttons, player instructions, and item descriptions.

Here's an example TypeScript code snippet that accomplishes this:

typescript// Define a type for the localization data structure
type LocalizationData = {
  language: string;
  translations: {
    startButton: string;
    exitButton: string;
    instructions: string;
    itemDescriptions: {
      sword: string;
      potion: string;
    };
  };
  colors: {
    buttonTextColor: string;
    itemColor: {
      sword: string;
      potion: string;
    };
  };
};

// English localization data
const enLocalization: LocalizationData = {
  language: 'English',
  translations: {
    startButton: 'Start',
    exitButton: 'Exit',
    instructions: 'Use arrow keys to move',
    itemDescriptions: {
      sword: 'A sharp blade',
      potion: 'Heals 20 health points',
    },
  },
  colors: {
    buttonTextColor: '#FFFFFF', // White
    itemColor: {
      sword: '#C0C0C0', // Silver
      potion: '#FF0000', // Red
    },
  },
};

// Chinese localization data
const zhLocalization: LocalizationData = {
  language: '中文',
  translations: {
    startButton: '开始',
    exitButton: '退出',
    instructions: '使用箭头键移动',
    itemDescriptions: {
      sword: '锋利的剑',
      potion: '治疗20生命值',
    },
  },
  colors: {
    buttonTextColor: '#FFFFFF', // White remains the same for simplicity
    itemColor: {
      sword: '#FFD700', // Gold
      potion: '#00FF00', // Green
    },
  },
};

// A function to retrieve localization data based on the current language setting
function getLocalization(language: 'en' | 'zh'): LocalizationData {
  if (language === 'en') {
    return enLocalization;
  } else if (language === 'zh') {
    return zhLocalization;
  } else {
    throw new Error('Unsupported language');
  }
}

// Example usage
const currentLanguage: 'en' | 'zh' = 'en'; // This could be dynamically determined
const localizationData = getLocalization(currentLanguage);
console.log(localizationData.translations.startButton); // Outputs: Start (or 开始 in Chinese)

This example defines a LocalizationData type that includes both translations and color customizations for UI elements and items within a game. By using this structure, you can easily extend the localization support to include more languages or additional elements needing localization, offering a scalable solution for game development.

Prompt:

Nice, now add support for Farsi.

Response:

To add support for Farsi (Persian) to the localization system, we'll extend the example with a new set of translations and color customizations for Farsi. Farsi is written in the Persian script, which is right-to-left, so keep in mind that some UI elements might need additional adjustments for such languages, but for simplicity, we'll focus on the text and colors here.

typescript// Extending the LocalizationData type to include Farsi
const faLocalization: LocalizationData = {
  language: 'فارسی',
  translations: {
    startButton: 'شروع',
    exitButton: 'خروج',
    instructions: 'برای حرکت از کلیدهای جهت‌دار استفاده کنید',
    itemDescriptions: {
      sword: 'یک شمشیر تیز',
      potion: '20 امتیاز سلامتی را درمان می‌کند',
    },
  },
  colors: {
    buttonTextColor: '#FFFFFF', // White can be universal
    itemColor: {
      sword: '#A52A2A', // Brown, considering cultural preferences or visibility
      potion: '#8A2BE2', // BlueViolet, for variety and visibility
    },
  },
};

// Update the getLocalization function to include Farsi
function getLocalization(language: 'en' | 'zh' | 'fa'): LocalizationData {
  switch (language) {
    case 'en':
      return enLocalization;
    case 'zh':
      return zhLocalization;
    case 'fa':
      return faLocalization;
    default:
      throw new Error('Unsupported language');
  }
}

// Example usage
const currentLanguage: 'en' | 'zh' | 'fa' = 'fa'; // Dynamically determined, example with Farsi
const localizationData = getLocalization(currentLanguage);
console.log(localizationData.translations.startButton); // Outputs: شروع

By adding the faLocalization object, we've included Farsi in the localization system. This approach maintains a consistent structure for adding new languages, enabling easy expansion and updates to the game's internationalization features. Remember, when implementing right-to-left languages in actual game UIs, additional considerations for text alignment and layout may be necessary to ensure a user-friendly experience.

Prompt:

For the Chinese translation, help me verify the meaning of each translated string by breaking down the meaning of each word in the phrase.

Response:

To verify the meaning of each translated string in the Chinese localization, let's break down the meaning of each word in the phrases provided. Chinese is a language where meaning can greatly depend on context, and each character often has a standalone meaning. Here's a breakdown of the translations provided in the Chinese localization example:

Start Button - 开始 (kāishǐ)

  • 开 (kāi): Open or start
  • 始 (shǐ): Begin or origin

Combined, 开始 means "to start" or "to begin", making it an appropriate translation for a "Start" button.

Exit Button - 退出 (tuìchū)

  • 退 (tuì): Retreat or withdraw
  • 出 (chū): Out or exit

Together, 退出 translates directly to "exit" or "withdraw", suitable for an "Exit" button.

Instructions - 使用箭头键移动 (shǐyòng jiàntóu jiàn yídòng)

  • 使用 (shǐyòng): Use or employ
  • 箭头 (jiàntóu): Arrowhead, often used to refer to arrow (keys)
  • 键 (jiàn): Key (as in keyboard)
  • 移动 (yídòng): Move or movement

So, 使用箭头键移动 translates to "Use arrow keys to move", breaking down as using the arrow keys for movement.

Item Descriptions:

Sword - 锋利的剑 (fēnglì de jiàn)

  • 锋利 (fēnglì): Sharp
  • 的 (de): (possessive/article particle, linking modifier with noun)
  • 剑 (jiàn): Sword

锋利的剑 means "A sharp sword", accurately describing a sword's key characteristic.

Potion - 治疗20生命值 (zhìliáo 20 shēngmìng zhí)

  • 治疗 (zhìliáo): Treat or heal
  • 20: Twenty (numerical value)
  • 生命值 (shēngmìng zhí): Health points or life value

治疗20生命值 translates to "Heals 20 health points", clearly describing a potion's effect.

This breakdown helps verify the meaning of each translated string, ensuring the Chinese localization accurately reflects the intended functions and descriptions in the game.

Prompt:

Cool, do it for fa too.

Response:

To verify the Farsi (Persian) translations and understand the meaning of each phrase, let's break down the translated strings provided in the Farsi localization. Farsi is an Indo-European language written in the Persian script, which is right-to-left. Each phrase and its components carry specific meanings, often with a rich cultural context. Here's the breakdown:

Start Button - شروع (Shoroo)

  • شروع (Shoroo): Start or begin

This word directly translates to "start" or "begin," making it an appropriate translation for a "Start" button.

Exit Button - خروج (Khorooj)

  • خروج (Khorooj): Exit or departure

The word خروج directly translates to "exit," suitable for an "Exit" button.

Instructions - برای حرکت از کلیدهای جهت‌دار استفاده کنید (Baraye Harkat Az Kelidhaye Jahat-Dar Estefadeh Konid)

  • برای (Baraye): For
  • حرکت (Harkat): Movement or move
  • از (Az): From or of
  • کلیدهای (Kelidhaye): Keys (plural form)
  • جهت‌دار (Jahat-Dar): Directional
  • استفاده کنید (Estefadeh Konid): Use (imperative form)

So, the phrase برای حرکت از کلیدهای جهت‌دار استفاده کنید translates to "For moving, use the directional keys," which is a practical instruction for using arrow keys to move.

Item Descriptions:

Sword - یک شمشیر تیز (Yek Shamshir Tiz)

  • یک (Yek): One or a
  • شمشیر (Shamshir): Sword
  • تیز (Tiz): Sharp

یک شمشیر تیز means "A sharp sword," accurately describing a sword's characteristic.

Potion - 20 امتیاز سلامتی را درمان می‌کند (20 Emteyaz Salamati Ra Darman Mikonad)

  • 20: Twenty (numerical value)
  • امتیاز سلامتی (Emteyaz Salamati): Health points (literally "health score")
  • را (Ra): (direct object marker)
  • درمان می‌کند (Darman Mikonad): Heals or cures

The phrase 20 امتیاز سلامتی را درمان می‌کند translates to "Heals 20 health points," describing the potion's effect in the game.

These breakdowns help ensure that the Farsi translations convey the correct meanings and functions intended for the game elements, matching the original English descriptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment