Skip to main content

Internationalization

Ryan SULess than 1 minute

The project supports multi-language internationalization, mainly divided into front-end internationalization and back-end internationalization

Front-end internationalization

The front-end internationalization files are located in src/locales/*, and the front-end static text translation is internationalized by the front-end

Called in vue or ts file

const { t } = useI18n();

// Usage
const name = t("sys.dictionary.dictionaryList");

Detail Tutorialopen in new window

Backend internationalization

The internationalization files are located in the i18n folder. The backend internationalization is used to translate all dynamic data from the backend. The directory structure in core is

├── locale
│   ├── en.json
│   └── zh.json
├── translator.go
├── translator_test.go
└── var.go

Just add translations in en.json and zh.json

Initialization method

trans := i18n.NewTranslator(i18n2.LocaleFS)

Usage

l.svcCtx.Trans.Trans(l.ctx, i18n.Success)

Reading External Files

Starting from version v1.0.7, it is possible to read external i18n files.

    var trans *i18n.Translator
	if c.I18nConf.Dir != "" {
		trans = i18n.NewTranslatorFromFile(c.I18nConf)
	} else {
		trans = i18n.NewTranslator(i18n2.LocaleFS)
	}

To read an external file, use i18n.NewTranslatorFromFile(c.I18nConf).

Load from I18nConf

trans.AddLanguagesByConf(c.I18nConf, i18n2.LocaleFS)