<commit_before>/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (<EMAIL>).
 * See LICENSE for details.
 */

package com.almasb.fxgl.localization

import com.almasb.fxgl.core.EngineService
import com.almasb.sslogger.Logger
import javafx.beans.binding.Bindings
import javafx.beans.binding.StringBinding
import javafx.beans.property.SimpleObjectProperty
import java.util.*
import java.util.concurrent.Callable

/**
 *
 * @author <NAME> (<EMAIL>)
 */
class LocalizationService : EngineService() {

    private val log = Logger.get(javaClass)

    private val languagesData = hashMapOf<Language, HashMap<String, String>>()

    private val selectedLanguageProp = SimpleObjectProperty(Language.NONE)

    fun selectedLanguageProperty() = selectedLanguageProp

    var selectedLanguage: Language
        get() = selectedLanguageProp.value
        set(value) { selectedLanguageProp.value = value }

    fun addLanguageData(lang: Language, bundle: ResourceBundle) {
        val map = languagesData[lang] ?: hashMapOf()
        bundle.keySet().forEach {
            map[it] = bundle.getString(it)
        }
        languagesData[lang] = map
    }

    fun addLanguageData(lang: Language, data: Map<String, String>) {
        val map = languagesData[lang] ?: hashMapOf()
        map.putAll(data)
        languagesData[lang] = map
    }

    /**
     * @return binding to a string translated to given language
     */
    fun localizedStringProperty(key: String): StringBinding {
        return Bindings.createStringBinding(Callable { getLocalizedString(key) }, selectedLanguageProp)
    }

    /**
     * @return a string translated to given language
     */
    fun getLocalizedString(key: String): String {
        return getLocalizedString(key, selectedLanguage)
    }

    /**
     * @return a string translated to given language
     */
    fun getLocalizedString(key: String, lang: Language): String {
        if (lang !in languagesData) {
            log.warning("No data for language $lang")
            return "MISSING_LANG!"
        }<commit_msg>reduce cyclomatic complexity of addLanguageData()<commit_after>/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (<EMAIL>).
 * See LICENSE for details.
 */

package com.almasb.fxgl.localization

import com.almasb.fxgl.core.EngineService
import com.almasb.sslogger.Logger
import javafx.beans.binding.Bindings
import javafx.beans.binding.StringBinding
import javafx.beans.property.SimpleObjectProperty
import java.util.*
import java.util.concurrent.Callable

/**
 *
 * @author <NAME> (<EMAIL>)
 */
class LocalizationService : EngineService() {

    private val log = Logger.get(javaClass)

    private val languagesData = hashMapOf<Language, HashMap<String, String>>()

    private val selectedLanguageProp = SimpleObjectProperty(Language.NONE)

    fun selectedLanguageProperty() = selectedLanguageProp

    var selectedLanguage: Language
        get() = selectedLanguageProp.value
        set(value) { selectedLanguageProp.value = value }

    fun addLanguageData(lang: Language, bundle: ResourceBundle) {
        val map = languagesData.getOrDefault(lang, hashMapOf())
        bundle.keySet().forEach {
            map[it] = bundle.getString(it)
        }
        languagesData[lang] = map
    }

    fun addLanguageData(lang: Language, data: Map<String, String>) {
        val map = languagesData.getOrDefault(lang, hashMapOf())
        map.putAll(data)
        languagesData[lang] = map
    }

    /**
     * @return binding to a string translated to given language
     */
    fun localizedStringProperty(key: String): StringBinding {
        return Bindings.createStringBinding(Callable { getLocalizedString(key) }, selectedLanguageProp)
    }

    /**
     * @return a string translated to given language
     */
    fun getLocalizedString(key: String): String {
        return getLocalizedString(key, selectedLanguage)
    }

    /**
     * @return a string translated to given language
     */
    fun getLocalizedString(key: String, lang: Language): String {
        if (lang !in languagesData) {
            log.warning("No data for language $lang")
            return "MISSING_LANG!"
        }
{'fraction_non_alphanumeric': 0.10208926875593542, 'fraction_numerical': 0.0, 'mean_word_length': 3.1754212091179386, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5221229', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1147, 'n_words': 296}