<commit_before>package i18n

import (
	"fmt"
	"os"

	"github.com/cloudfoundry/cli/cf/configuration/core_config"
	resources "github.com/cloudfoundry/cli/cf/resources"
	go_i18n "github.com/nicksnyder/go-i18n/i18n"
)

const DEFAULT_LOCALE = "en_US"

var T go_i18n.TranslateFunc

var SUPPORTED_LOCALES = map[string]string{
	"de": "de_DE",
	"en": "en_US",
	"es": "es_ES",
	"fr": "fr_FR",
	"it": "it_IT",
	"ja": "ja_JA",
	"ko": "ko_KR",
	"pt": "pt_BR",
	//"ru": "ru_RU", - Will add support for Russian when nicksnyder/go-i18n supports Russian
	"zh": "zh_Hans",
}

func Init(config core_config.Reader) go_i18n.TranslateFunc {
	for _, assetName := range resources.AssetNames() {
		assetBytes, err := resources.Asset(assetName)
		if err != nil {
			panic(fmt.Sprintf("Could not load asset '%s': %s", assetName, err.Error()))
		}
		err = go_i18n.ParseTranslationFileBytes(assetName, assetBytes)
		if err != nil {
			panic(fmt.Sprintf("Could not load translations '%s': %s", assetName, err.Error()))
		}
	}

	T, err := go_i18n.Tfunc(config.Locale(), os.Getenv("LC_ALL"), os.Getenv("LANG"), DEFAULT_LOCALE)
	if err != nil {
		panic(fmt.Sprintf("Failed to create translation func", err.Error()))
	}

	return T
}<commit_msg>Improve performance of i18n.Init<commit_after>package i18n

import (
	"fmt"
	"os"
	"path"
	"strings"

	"github.com/cloudfoundry/cli/cf/configuration/core_config"
	"github.com/cloudfoundry/cli/cf/resources"
	go_i18n "github.com/nicksnyder/go-i18n/i18n"
	"github.com/nicksnyder/go-i18n/i18n/language"
)

const DEFAULT_LOCALE = "en_US"

var T go_i18n.TranslateFunc

var SUPPORTED_LOCALES = map[string]string{
	"de": "de_DE",
	"en": "en_US",
	"es": "es_ES",
	"fr": "fr_FR",
	"it": "it_IT",
	"ja": "ja_JA",
	"ko": "ko_KR",
	"pt": "pt_BR",
	//"ru": "ru_RU", - Will add support for Russian when nicksnyder/go-i18n supports Russian
	"zh": "zh_Hans",
}

func Init(config core_config.Reader) go_i18n.TranslateFunc {
	sources := []string{
		config.Locale(),
		os.Getenv("LC_ALL"),
		os.Getenv("LANG"),
		DEFAULT_LOCALE,
	}

	assetNames := resources.AssetNames()

	for _, source := range sources {
		if source == "" {
			continue
		}

		for _, l := range language.Parse(source) {
			for _, assetName := range assetNames {
				assetLocale := strings.ToLower(strings.Replace(path.Base(assetName), "_", "-", -1))
				if strings.HasPrefix(assetLocale, l.Tag) {
					assetBytes, err := resources.Asset(assetName)
					if err != nil {
						panic(fmt.Sprintf("Could not load asset '%s': %s", assetName, err.Error()))
					}

					err = go_i18n.ParseTranslationFileBytes(assetName, assetBytes)
					if err != nil {
						panic(fmt.Sprintf("Could not load translations '%s': %s", assetName, err.Error()))
					}

					T, err := go_i18n.Tfunc(source)
					if err == nil {
						return T
					}
				}
			}
		}
	}

	panic("Unable to find suitable translation")
}
{'fraction_non_alphanumeric': 0.16812916812916812, 'fraction_numerical': 0.015093015093015093, 'mean_word_length': 4.026455026455026, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1625237', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1097, 'n_words': 228}