i18n

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package i18n provides a foundational implementation of a translation service using the Go x/text package. This service facilitates text translation for applications, leveraging JSON format for translation files. It is designed with the flexibility to be extended to support other translation data sources such as databases or remote services in the future.

Key Features: - Support for JSON-based translation files. - Easy integration with context-based locale determination. - Dynamic placeholder substitution in translation strings.

Index

Constants

View Source
const (
	LocaleEnUS string = "en-US"
	LocaleEnGB string = "en-GB"
	LocaleEnIN string = "en-IN"
	LocaleZhCN string = "zh-CN"
)

Locale name constants

Variables

View Source
var CurrencyMap = map[string]string{}/* 247 elements not displayed */

Locale/Language to currency map

View Source
var DateFormatMap = map[string]string{}/* 210 elements not displayed */

Locale to date format string map

Functions

func FixLocaleFormat

func FixLocaleFormat(locale string) string

FixLocaleFormat corrects locale string formats, replacing underscores with dashes. This standardization ensures consistency in locale identification.

Parameters:

  • locale: The locale string to correct.

Returns:

  • A standardized locale string with underscores replaced by dashes.

Usage:

  • Use this function to ensure locale strings are formatted consistently across the application.

func Init

func Init(config *I18nConfig)

Init initializes the internationalization system with the specified configuration. It sets up the necessary infrastructure based on the provided settings.

Parameters:

  • config: A configuration object containing settings such as default locale, supported locales, and translations.

Usage:

  • Call this function at application startup to configure and enable the internationalization capabilities.

func IsI18nEnabled

func IsI18nEnabled() bool

IsI18nEnabled returns a boolean indicating whether the internationalization service is currently enabled.

Returns:

  • true if i18n is enabled, false otherwise.

Usage:

  • Use this function to check if internationalization features should be utilized in the application.

func T

func T(ctx context.Context, key string, placeholders map[string]interface{}) string

T translates a key into the corresponding text for the specified locale in the provided context. If placeholders are provided, it dynamically replaces them in the translated string.

Parameters:

  • ctx: Context containing locale information, usually derived from incoming requests.
  • key: The key corresponding to the text that needs to be translated.
  • placeholders: A map of placeholder keys and their respective values to be substituted in the translation.

Returns:

  • The translated string with placeholders substituted, or the key itself if translation cannot be performed.

Usage:

  • Use this function to fetch a localized version of text based on the user's locale, automatically substituting any dynamic content.

Types

type I18nConfig

type I18nConfig struct {
	IsEnabled        bool
	DefaultLocale    string
	SupportedLocales []string
	Translations     map[string]map[string]string
}

I18nConfig holds the configuration settings for the internationalization (i18n) service. This struct includes settings to enable or disable i18n, specify the default and supported locales, and provide the necessary translations for each locale.

Fields:

  • IsEnabled: A boolean indicating whether the i18n service should be enabled. If false, the i18n functionalities will not be initialized or used, and the service may bypass translation logic to optimize performance.

  • DefaultLocale: The default locale to use when no specific locale is provided or detected. This locale's translations are used as fallbacks if a translation is not available in the user's current locale.

  • SupportedLocales: A list of locales that the application explicitly supports. This list is used to validate locale requests and to load the corresponding translations. Only locales listed here will have their translations loaded into the system.

  • Translations: A map where each key is a locale identifier and its value is another map of translation keys and their corresponding localized strings. This nested map structure allows easy access to any specific translation based on a locale and a translation key.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL