Skip to content

Commit

Permalink
Merge branch 'v3.6/next' into v3.6/213
Browse files Browse the repository at this point in the history
  • Loading branch information
YiiGuxing committed Sep 17, 2024
2 parents 35c7047 + 1380a7a commit 1593da7
Show file tree
Hide file tree
Showing 32 changed files with 185 additions and 108 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## [Unreleased]

- DeepL Translator now supports languages such as Chinese Traditional.
- User interaction experience optimization and improvement.
- Bug fixes.
- DeepL 翻译现已支持繁体中文等语言
- 用户交互体验优化与改进
- Bug 修复

## [3.6.4] (2024/07/23)

- The GPT-4o mini is now available in the OpenAI Translator engine.
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pluginGroup = cn.yiiguxing.plugin.translate
pluginRepositoryUrl = https://github.com/YiiGuxing/TranslationPlugin

# SemVer format -> https://semver.org
pluginMajorVersion = 3.6.4
pluginMajorVersion = 3.6.5
pluginPreReleaseVersion =
pluginBuildMetadata =
autoSnapshotVersion = true
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/cn/yiiguxing/plugin/translate/Contract.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
package cn.yiiguxing.plugin.translate

import cn.yiiguxing.plugin.translate.trans.Lang
import cn.yiiguxing.plugin.translate.trans.SupportedLanguagesData
import cn.yiiguxing.plugin.translate.trans.Translation
import cn.yiiguxing.plugin.translate.trans.Translator
import com.intellij.openapi.Disposable

data class SupportedLanguages(val source: List<Lang>, val target: List<Lang>)

interface Presenter {

val translator: Translator
Expand All @@ -27,7 +26,7 @@ interface Presenter {
/**
* 已支持的语言
*/
val supportedLanguages: SupportedLanguages
val supportedLanguages: SupportedLanguagesData

/**
* 检测指定的源语言是否被支持
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class TranslationPresenter(private val view: View, private val recordHistory: Bo

override val primaryLanguage: Lang get() = translator.primaryLanguage

override val supportedLanguages: SupportedLanguages
override val supportedLanguages: SupportedLanguagesData
get() = with(translator) {
SupportedLanguages(supportedSourceLanguages, supportedTargetLanguages)
SupportedLanguagesData(supportedSourceLanguages, supportedTargetLanguages)
}

override fun isSupportedSourceLanguage(sourceLanguage: Lang): Boolean {
Expand All @@ -44,6 +44,7 @@ class TranslationPresenter(private val view: View, private val recordHistory: Bo
return when (settings.targetLanguageSelection) {
DEFAULT -> Lang.AUTO.takeIf { isSupportedTargetLanguage(it) }
?: if (text.isEmpty() || text.any(NON_LATIN_CONDITION)) Lang.ENGLISH else primaryLanguage

PRIMARY_LANGUAGE -> primaryLanguage
LAST -> states.lastLanguages.target.takeIf { isSupportedTargetLanguage(it) } ?: primaryLanguage
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ package cn.yiiguxing.plugin.translate.trans
*/
interface LanguageAdapter {

/**
* List of supported source languages.
*/
val supportedSourceLanguages: List<Lang>

/**
* List of supported target languages.
*/
val supportedTargetLanguages: List<Lang>

/**
* Returns the [language][Lang] corresponding to the given [code].
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,17 @@ enum class Lang(
CHICHEWA("chichewa", "ny"),

/** 中文 */
CHINESE("chinese", "zh-CN"),
CHINESE("chinese", "zh"),

/** 粤语 */
CHINESE_CANTONESE("chinese.cantonese", "zh-CANTONESE"),

/** 文言文 */
CHINESE_CLASSICAL("chinese.classical", "zh-CLASSICAL"),

/** 中文(简体) */
CHINESE_SIMPLIFIED("chinese.simplified", "zh-CN"),

/** 中文(繁体) */
CHINESE_TRADITIONAL("chinese.traditional", "zh-TW"),

Expand Down Expand Up @@ -459,7 +462,7 @@ enum class Lang(
return when (localeLanguage) {
Locale.CHINESE.language -> when (Locale.getDefault().country) {
"HK", "TW" -> CHINESE_TRADITIONAL
else -> CHINESE
else -> CHINESE_SIMPLIFIED
}

else -> values().find { Locale(it.code).language.equals(localeLanguage, ignoreCase = true) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.yiiguxing.plugin.translate.trans

/**
* Supported languages.
*/
interface SupportedLanguages {
/**
* The supported source languages.
*/
val sourceLanguages: List<Lang>

/**
* The supported target languages.
*/
val targetLanguages: List<Lang>
}

data class SupportedLanguagesData(
override val sourceLanguages: List<Lang>,
override val targetLanguages: List<Lang>
) : SupportedLanguages
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package cn.yiiguxing.plugin.translate.trans.ali

import cn.yiiguxing.plugin.translate.trans.BaseLanguageAdapter
import cn.yiiguxing.plugin.translate.trans.Lang
import cn.yiiguxing.plugin.translate.trans.SupportedLanguages

/**
* Language adapter for Ali Translator.
*/
object AliLanguageAdapter : BaseLanguageAdapter() {
object AliLanguageAdapter : BaseLanguageAdapter(), SupportedLanguages {

override val supportedSourceLanguages: List<Lang> = listOf(
override val sourceLanguages: List<Lang> = listOf(
Lang.AUTO,
Lang.CHINESE,
Lang.CHINESE_SIMPLIFIED,
Lang.CHINESE_TRADITIONAL,
Lang.ENGLISH,
Lang.JAPANESE,
Expand All @@ -30,8 +31,8 @@ object AliLanguageAdapter : BaseLanguageAdapter() {
Lang.HINDI
)

override val supportedTargetLanguages: List<Lang> = listOf(
Lang.CHINESE,
override val targetLanguages: List<Lang> = listOf(
Lang.CHINESE_SIMPLIFIED,
Lang.ENGLISH,
Lang.JAPANESE,
Lang.KOREAN,
Expand All @@ -49,7 +50,7 @@ object AliLanguageAdapter : BaseLanguageAdapter() {
)

override fun getAdaptedLanguages(): Map<String, Lang> = mapOf(
"zh" to Lang.CHINESE,
"zh" to Lang.CHINESE_SIMPLIFIED,
"zh-tw" to Lang.CHINESE_TRADITIONAL,
"yue" to Lang.CHINESE_CANTONESE,
"fil" to Lang.FILIPINO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ object AliTranslator : AbstractTranslator(), DocumentationTranslator {
override val primaryLanguage: Lang
get() = ALI.primaryLanguage

override val supportedSourceLanguages: List<Lang> = AliLanguageAdapter.supportedSourceLanguages
override val supportedSourceLanguages: List<Lang> = AliLanguageAdapter.sourceLanguages

override val supportedTargetLanguages: List<Lang> = AliLanguageAdapter.supportedTargetLanguages
override val supportedTargetLanguages: List<Lang> = AliLanguageAdapter.targetLanguages

override fun checkConfiguration(force: Boolean): Boolean {
if (force ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package cn.yiiguxing.plugin.translate.trans.baidu

import cn.yiiguxing.plugin.translate.trans.BaseLanguageAdapter
import cn.yiiguxing.plugin.translate.trans.Lang
import cn.yiiguxing.plugin.translate.trans.SupportedLanguages

/**
* Language adapter for Baidu Translator.
*/
object BaiduLanguageAdapter : BaseLanguageAdapter() {
object BaiduLanguageAdapter : BaseLanguageAdapter(), SupportedLanguages {

/** 通用版支持的语言列表 */
private val SUPPORTED_LANGUAGES: List<Lang> = listOf(
Lang.CHINESE,
Lang.CHINESE_SIMPLIFIED,
Lang.ENGLISH,
Lang.CHINESE_TRADITIONAL,
Lang.CHINESE_CANTONESE,
Expand Down Expand Up @@ -43,7 +44,7 @@ object BaiduLanguageAdapter : BaseLanguageAdapter() {
/** 尊享版支持的语言列表 */
@Suppress("unused")
private val SUPPORTED_LANGUAGES_PRO: List<Lang> = listOf(
Lang.CHINESE,
Lang.CHINESE_SIMPLIFIED,
Lang.ENGLISH,
Lang.CHINESE_TRADITIONAL,
Lang.CHINESE_CANTONESE,
Expand Down Expand Up @@ -123,11 +124,11 @@ object BaiduLanguageAdapter : BaseLanguageAdapter() {
Lang.ARMENIAN,
)

override val supportedSourceLanguages: List<Lang> = SUPPORTED_LANGUAGES
override val sourceLanguages: List<Lang> = SUPPORTED_LANGUAGES
.toMutableList()
.apply { add(0, Lang.AUTO) }

override val supportedTargetLanguages: List<Lang> = SUPPORTED_LANGUAGES
override val targetLanguages: List<Lang> = SUPPORTED_LANGUAGES

override fun getAdaptedLanguages(): Map<String, Lang> = mapOf(
"afr" to Lang.AFRIKAANS,
Expand All @@ -141,7 +142,7 @@ object BaiduLanguageAdapter : BaseLanguageAdapter() {
"ben" to Lang.BENGALI,
"bos" to Lang.BOSNIAN,
"bul" to Lang.BULGARIAN,
"zh" to Lang.CHINESE,
"zh" to Lang.CHINESE_SIMPLIFIED,
"cht" to Lang.CHINESE_TRADITIONAL,
"wyw" to Lang.CHINESE_CLASSICAL,
"yue" to Lang.CHINESE_CANTONESE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ object BaiduTranslator : AbstractTranslator() {
override val primaryLanguage: Lang
get() = BAIDU.primaryLanguage

override val supportedSourceLanguages: List<Lang> = BaiduLanguageAdapter.supportedSourceLanguages
override val supportedSourceLanguages: List<Lang> = BaiduLanguageAdapter.sourceLanguages

override val supportedTargetLanguages: List<Lang> = BaiduLanguageAdapter.supportedTargetLanguages
override val supportedTargetLanguages: List<Lang> = BaiduLanguageAdapter.targetLanguages

private val errorMessageMap: Map<Int, String> by lazy {
mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.intellij.util.io.RequestBuilder
class DeeplService(var authKey: String) {

/**
* Determines if the given [DeepL Authentication Key][authKey] belongs to an API Free account.
* Determines if the given [DeepL Authentication Key][authKey] belongs to an API-Free account.
*/
val isFreeAccount: Boolean get() = isFreeAccountAuthKey(authKey)

Expand All @@ -27,7 +27,7 @@ class DeeplService(var authKey: String) {
private fun RequestBuilder.auth() {
userAgent(Http.PLUGIN_USER_AGENT)
// Authentication method should be header-based authentication,
// auth-key will leak into the log file if it is authenticated as a parameter.
// the auth-key will leak into the log file if it is authenticated as a parameter.
tuner { it.setRequestProperty("Authorization", "DeepL-Auth-Key $authKey") }
}

Expand All @@ -36,11 +36,11 @@ class DeeplService(var authKey: String) {
*/
fun translate(text: String, sourceLang: Lang, targetLang: Lang, isDocument: Boolean): String {
val params: LinkedHashMap<String, String> = linkedMapOf(
"target_lang" to targetLang.deeplLanguageCode,
"target_lang" to targetLang.deeplLanguageCodeForTarget,
"text" to text
)
if (sourceLang !== Lang.AUTO) {
params["source_lang"] = sourceLang.deeplLanguageCode
params["source_lang"] = sourceLang.deeplLanguageCodeForSource
}
if (isDocument) {
params["tag_handling"] = "html"
Expand Down Expand Up @@ -80,10 +80,10 @@ class DeeplService(var authKey: String) {
private const val DEEPL_SERVER_URL_PRO = "https://api.deepl.com"

/**
* Determines if the given [DeepL Authentication Key][authKey] belongs to an API Free account.
* Determines if the given [DeepL Authentication Key][authKey] belongs to an API-Free account.
*
* @param authKey DeepL Authentication Key as found in your [DeepL account](https://www.deepl.com/pro-account/).
* @return `true` if the Authentication Key belongs to an API Free account, otherwise `false`.
* @return `true` if the Authentication Key belongs to an API-Free account, otherwise `false`.
*/
fun isFreeAccountAuthKey(authKey: String): Boolean = authKey.endsWith(":fx")
}
Expand Down
Loading

0 comments on commit 1593da7

Please sign in to comment.