翻译修复-未完成

This commit is contained in:
coldmint 2022-04-06 09:00:03 +08:00
parent 086a35ac8e
commit fd631149ef
11 changed files with 46 additions and 21 deletions

View File

@ -31,7 +31,7 @@ android {
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 32 targetSdkVersion 32
versionCode 13 versionCode 13
versionName "2.0 Release 6(2022-3-26)" versionName "2.0 Release 7(2022-4-5)"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }

View File

@ -23,15 +23,11 @@ import java.util.*
abstract class BaseActivity<ViewBingType : ViewBinding> : abstract class BaseActivity<ViewBingType : ViewBinding> :
AppCompatActivity() { AppCompatActivity() {
// abstract fun getViewModelObject(viewModelProvider: ViewModelProvider): ViewModelType
abstract fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) abstract fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean)
abstract fun getViewBindingObject(): ViewBingType abstract fun getViewBindingObject(): ViewBingType
// protected val viewModel: ViewModelType by lazy {
// getViewModelObject(ViewModelProvider(this))
// }
protected val viewBinding: ViewBingType by lazy { protected val viewBinding: ViewBingType by lazy {
getViewBindingObject() getViewBindingObject()
@ -45,14 +41,15 @@ abstract class BaseActivity<ViewBingType : ViewBinding> :
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
// loadLanguage()
whenCreateActivity(savedInstanceState, false) whenCreateActivity(savedInstanceState, false)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(viewBinding.root) setContentView(viewBinding.root)
whenCreateActivity(savedInstanceState, true) whenCreateActivity(savedInstanceState, true)
} }
/** /**
* 展示Toast * 展示Toast
* @param str String * @param str String
@ -66,8 +63,6 @@ abstract class BaseActivity<ViewBingType : ViewBinding> :
*/ */
protected fun setReturnButton() { protected fun setReturnButton() {
if (supportActionBar != null) { if (supportActionBar != null) {
//激活返回按钮
supportActionBar!!.setHomeButtonEnabled(true)
//将显示主页设置为已启用 //将显示主页设置为已启用
supportActionBar!!.setDisplayHomeAsUpEnabled(true) supportActionBar!!.setDisplayHomeAsUpEnabled(true)
} }

View File

@ -161,12 +161,11 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
val tokenizer = StringTokenizer(code, split, true) val tokenizer = StringTokenizer(code, split, true)
//缓存翻译数据,以便加速重复数据的翻译 //缓存翻译数据,以便加速重复数据的翻译
val translationMap = HashMap<String, String>() val translationMap = HashMap<String, String>()
//保存是否为注释
var isNote = false
//保存每次代码的翻译结果 //保存每次代码的翻译结果
val codeResult = StringBuilder() val codeResult = StringBuilder()
//保存完整的翻译结果 //保存完整的翻译结果
val translationResult = StringBuilder() val translationResult = StringBuilder()
var codeBlockType = CompileConfiguration.CodeBlockType.Key
handler.post { handler.post {
translatorListener.beforeTranslate() translatorListener.beforeTranslate()
} }
@ -178,19 +177,26 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
codeResult.delete(0, codeResult.length) codeResult.delete(0, codeResult.length)
when (code) { when (code) {
"\n" -> { "\n" -> {
isNote = false codeBlockType = CompileConfiguration.CodeBlockType.Key
codeResult.append(code) codeResult.append(code)
} }
"\r" -> { "\r" -> {
} }
":", " ", ",", "(", ")", "=", "%", "{", "}", "+", "*", "/" -> codeResult.append( " ", ",", "(", ")", "=", "%", "{", "}", "+", "*", "/" -> codeResult.append(
code code
) )
else -> if (isNote) { ":" -> {
if (codeBlockType == CompileConfiguration.CodeBlockType.Key) {
codeBlockType =
CompileConfiguration.CodeBlockType.Value
}
codeResult.append(code)
}
else -> if (codeBlockType == CompileConfiguration.CodeBlockType.Note) {
codeResult.append(code) codeResult.append(code)
} else { } else {
if (code.startsWith("#")) { if (code.startsWith("#")) {
isNote = true codeBlockType = CompileConfiguration.CodeBlockType.Note
codeResult.append(code) codeResult.append(code)
} else if (code.startsWith("[") && code.endsWith("]")) { } else if (code.startsWith("[") && code.endsWith("]")) {
val symbolPosition = code.lastIndexOf("_") val symbolPosition = code.lastIndexOf("_")
@ -245,13 +251,23 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
codeResult.append(code) codeResult.append(code)
} }
} else { } else {
//是否需要检查值
if (codeBlockType == CompileConfiguration.CodeBlockType.Key) {
val type = getValueData(codeInfo.type)
val tag = type?.tag
if (!tag.isNullOrBlank()) {
//如果此类型为特殊标注,那么设置为注释
codeBlockType =
CompileConfiguration.CodeBlockType.Note
}
}
codeResult.append(codeInfo.translate) codeResult.append(codeInfo.translate)
} }
} }
} }
} }
//如果代码不是注释,也不是换行,那么缓存它。 //如果代码不是注释,也不是换行,不是冒号,那么缓存它。
if (!isNote && code != "\n") { if (codeBlockType != CompileConfiguration.CodeBlockType.Note && code != ":" && code != "\n") {
translationMap[code] = codeResult.toString() translationMap[code] = codeResult.toString()
} }
translationResult.append(codeResult.toString()) translationResult.append(codeResult.toString())
@ -324,12 +340,15 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
compileConfiguration.appendResult(translation) compileConfiguration.appendResult(translation)
} }
":" -> { ":" -> {
if (compileConfiguration.codeBlockType == CompileConfiguration.CodeBlockType.Value) { when (compileConfiguration.codeBlockType) {
CompileConfiguration.CodeBlockType.Value -> {
compileConfiguration.appendResult(translation) compileConfiguration.appendResult(translation)
} else { }
CompileConfiguration.CodeBlockType.Key -> {
compileConfiguration.codeBlockType = compileConfiguration.codeBlockType =
CompileConfiguration.CodeBlockType.Value CompileConfiguration.CodeBlockType.Value
} }
}
codeResult.append(translation) codeResult.append(translation)
} }
else -> if (compileConfiguration.codeBlockType == CompileConfiguration.CodeBlockType.Note) { else -> if (compileConfiguration.codeBlockType == CompileConfiguration.CodeBlockType.Note) {
@ -380,6 +399,16 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
codeResult.append(translation) codeResult.append(translation)
} }
} else { } else {
//是否需要检查值
if (compileConfiguration.codeBlockType == CompileConfiguration.CodeBlockType.Key) {
val type = getValueData(codeInfo.type)
val tag = type?.tag
if (!tag.isNullOrBlank()) {
//如果此类型为特殊标注,那么设置为注释
compileConfiguration.codeBlockType =
CompileConfiguration.CodeBlockType.Note
}
}
codeResult.append(codeInfo.code) codeResult.append(codeInfo.code)
} }
//翻译代码段完毕后,加入行结果集合 //翻译代码段完毕后,加入行结果集合

View File

@ -105,6 +105,7 @@ data class CompileConfiguration(
/** /**
* 代码块类 * 代码块类
* 注释变量名
*/ */
enum class CodeBlockType { enum class CodeBlockType {
Key, Value, Section, Note, VariableName Key, Value, Section, Note, VariableName