Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
wilinz committed Jul 31, 2022
1 parent 78350da commit 0eeb48c
Show file tree
Hide file tree
Showing 137 changed files with 693 additions and 506 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "com.wilinz.globalization"
version = "1.0.0"
version = "1.0.1"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@ import com.wilinz.globalization.translator.task.AndroidTranslateTask
import com.wilinz.globalization.translator.ui.dialog.androidTranslateDialog
import org.dom4j.Document

internal fun androidActionPerformed(e: AnActionEvent, file: VirtualFile, getDocument: ()->Document) {
internal fun androidActionPerformed(
e: AnActionEvent,
file: VirtualFile,
getDocument: () -> Document,
isShowOverwriteCheckBox: Boolean = false
) {
val resourceDir = file.parent.parent
if (!resourceDir.exists()) return

androidTranslateDialog(
project = e.project,
title = message("translate_this_file"),
file = file,
onOKClick = { sourceLanguage, targetLanguages ->
onOKClick = { config ->
AndroidTranslateTask(
project = e.project,
filename = file.name,
resourceDir = resourceDir,
document = getDocument(),
form = sourceLanguage,
to = targetLanguages,
form = config.sourceLanguage,
to = config.targetLanguages,
isOverwriteTargetFile = config.isOverwriteTargetFile,
title = message("translate_file", file.name)
).queue()
}
},
isShowOverwriteCheckBox = isShowOverwriteCheckBox
).show()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class TranslateAction : AnAction() {
getDocument = {
file.inputStream.use { SAXReader().read(it) }
},
isShowOverwriteCheckBox = true
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class TranslateSelectedAction : AnAction() {
getDocument = {
StringReader(xml).use { SAXReader().read(it) }
},
isShowOverwriteCheckBox = false
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import java.nio.charset.Charset
internal fun propertiesActionPerformed(
e: AnActionEvent,
file: VirtualFile,
getProperties: (charset: Charset) -> Properties
getProperties: (charset: Charset) -> Properties,
isShowOverwriteCheckBox: Boolean = false
) {
val basename = PropertiesUtil.getBaseName(file.name) ?: return
val resourceDir = file.parent
Expand All @@ -21,18 +22,20 @@ internal fun propertiesActionPerformed(
propertiesTranslateDialog(
project = e.project,
title = message("translate_this_file"),
onOKClick = { sourceLanguage, targetLanguages, isEncodeUnicode, charset ->
onOKClick = { config ->
PropertiesTranslateTask(
project = e.project,
baseFilename = basename,
isEncodeUnicode = isEncodeUnicode,
isEncodeUnicode = config.isEncodeUnicode,
resourceDir = resourceDir,
properties = getProperties(charset),
form = sourceLanguage,
to = targetLanguages,
properties = getProperties(config.charset),
form = config.sourceLanguage,
to = config.targetLanguages,
isOverwriteTargetFile = config.isOverwriteTargetFile,
title = message("translate_file", file.name)
).queue()
},
file = file
file = file,
isShowOverwriteCheckBox = isShowOverwriteCheckBox
).show()
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TranslateAction : AnAction() {
getProperties = { charset ->
file.inputStream.use { Properties.loadProperties(it.reader(charset)) }
},
isShowOverwriteCheckBox = true
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class TranslateSelectedAction : AnAction() {
getProperties = {
StringReader(selectedText.trim()).use { Properties.loadProperties(it) }
},
isShowOverwriteCheckBox = false
)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AndroidTranslateTask(
private val document: Document,
private val form: String,
private val to: List<String>,
private val isOverwriteTargetFile: Boolean,
title: @NlsContexts.ProgressTitle String
) : TranslateTask(project, title) {

Expand All @@ -27,6 +28,7 @@ class AndroidTranslateTask(
document = document,
form = form,
to = to,
isOverwriteTargetFile = isOverwriteTargetFile,
onEachStart = { index, language ->
onEachStart(indicator, index, language)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PropertiesTranslateTask(
private val properties: Properties,
private val form: String,
private val to: List<String>,
private val isOverwriteTargetFile: Boolean,
title: @NlsContexts.ProgressTitle String
) : TranslateTask(project, title) {
override fun run(indicator: ProgressIndicator) {
Expand All @@ -28,6 +29,7 @@ class PropertiesTranslateTask(
properties = properties,
form = form,
to = to,
isOverwriteTargetFile = isOverwriteTargetFile,
onEachStart = { index, language ->
onEachStart(indicator, index, language)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object AndroidXmlTranslator {
document: Document,
form: String,
to: List<String>,
isOverwriteTargetFile: Boolean,
onEachStart: ((index: Int, language: String) -> Unit)? = null,
onEachSuccess: ((index: Int, language: String) -> Unit)? = null,
onEachError: ((index: Int, language: String, error: Throwable) -> Unit)? = null,
Expand All @@ -30,6 +31,7 @@ object AndroidXmlTranslator {
translator = translator,
oldDocument = document,
newDocumentFetcher = { index, language ->
if (isOverwriteTargetFile) return@translate null
val stringsFile =
resourceDir.findChild(LanguageUtil.androidLanguageDirMap[language]!!)?.findChild(filename)
?: return@translate null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object PropertiesTranslator {
properties: Properties,
form: String,
to: List<String>,
isOverwriteTargetFile: Boolean,
onEachStart: ((index: Int, language: String) -> Unit)? = null,
onEachSuccess: ((index: Int, language: String) -> Unit)? = null,
onEachError: ((index: Int, language: String, error: Throwable) -> Unit)? = null,
Expand All @@ -26,6 +27,7 @@ object PropertiesTranslator {
translator = translator,
oldProperties = properties,
newPropertiesFetcher = { _, language ->
if (isOverwriteTargetFile) return@translate null
val childName = PropertiesUtil.getFilenameByBaseName(baseFilename, language)
return@translate resourceDir.findChild(childName)?.let {
it.inputStream.reader().use { input ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class GoogleTranslator : Translator {
val response = call.execute()
response.body?.string()?.let {
val result = JsonParser.parseString(it).asJsonArray.map { element ->
element.asString.converseResult().firstUppercase()
element.asString.converseResult().firstUppercase().trim()
}
printlnDebug(result)
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ fun androidTranslateDialog(
project: Project?,
title: String,
file: VirtualFile,
onOKClick: (sourceLanguage: String, targetLanguages: List<String>) -> Unit,
onOKClick: (config: TranslationConfig) -> Unit,
isShowOverwriteCheckBox: Boolean = false
): TranslateDialog {

val defaultSourceLanguage =
Expand All @@ -25,6 +26,7 @@ fun androidTranslateDialog(
title,
defaultSourceLanguage,
onOKClick,
translatedLanguages
translatedLanguages,
isShowOverwriteCheckBox
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,74 +20,47 @@ import com.wilinz.globalization.translator.util.PropertiesUtil
import java.nio.charset.Charset
import javax.swing.JComponent

class PropertiesTranslateConfig(
sourceLanguage: String,
targetLanguages: List<String>,
val isEncodeUnicode: Boolean,
val charset: Charset,
isOverwriteTargetFile: Boolean
) : TranslationConfig(sourceLanguage, targetLanguages, isOverwriteTargetFile)

class PropertiesTranslateDialog(
project: Project?,
title: String,
defaultSourceLanguage: String = "en",
private val onOKClick: (sourceLanguage: String, targetLanguages: List<String>, isEncodeUnicode: Boolean, sourceFileFormat: Charset) -> Unit,
isShowOverwriteCheckBox: Boolean,
private val onOKClick: (config: PropertiesTranslateConfig) -> Unit,
private val translatedLanguages: List<String> = listOf()
) : TranslateDialog(project, title, defaultSourceLanguage, null, translatedLanguages) {
) : TranslateDialog(project, title, defaultSourceLanguage, null, translatedLanguages, isShowOverwriteCheckBox) {

private var isEncodeUnicode by mutableStateOf(false)
private var fileFormat by mutableStateOf(Charsets.UTF_8.name())

init {
super.setOnOKClickListener { sourceLanguage, targetLanguages ->
onOKClick(sourceLanguage, targetLanguages, isEncodeUnicode, charset(fileFormat))
super.setOnOKClickListener { config ->
onOKClick(
PropertiesTranslateConfig(
sourceLanguage = config.sourceLanguage,
targetLanguages = config.targetLanguages,
isOverwriteTargetFile = config.isOverwriteTargetFile,
isEncodeUnicode = isEncodeUnicode,
charset = charset(fileFormat)
)
)
}
}

override fun createCenterPanel(): JComponent {
return ComposePanel().apply {
setBounds(0, 0, 1000, 600)
setContent {
WidgetTheme(darkTheme = true) {
Surface(modifier = Modifier.fillMaxSize()) {
Box(modifier = Modifier.padding(8.dp)) {
PropertiesDialogContent(
fileFormat = fileFormat,
onFileFormatChange = { fileFormat = it },
isEncodeUnicode = isEncodeUnicode,
onIsEncodeUnicodeChange = { isEncodeUnicode = it },
sourceLanguage = sourceLanguage,
isTranslated = { language ->
translatedLanguages.indexOf(language) != -1
},
onSourceLanguageChange = {
sourceLanguage = it
},
targetLanguages = targetLanguages
)
}
}
}
}
return setCenterPanel {
SourceFileFormat(format = fileFormat, onFormatChange = { fileFormat = it })
EncodeUnicodeCkeckBox(isEncodeUnicode = isEncodeUnicode, onIsEncodeUnicodeChange = { isEncodeUnicode = it })
}
}

@Composable
private fun PropertiesDialogContent(
fileFormat: String,
onFileFormatChange: (language: String) -> Unit,
isEncodeUnicode: Boolean,
onIsEncodeUnicodeChange: (Boolean) -> Unit,
sourceLanguage: String,
onSourceLanguageChange: (language: String) -> Unit,
targetLanguages: SnapshotStateList<LanguageItem>,
isTranslated: (language: String) -> Boolean
) {
Column {
SourceFileFormat(fileFormat, onFileFormatChange)
SourceLanguage(sourceLanguage, onSourceLanguageChange)
Row {
TargetLanguage(targetLanguages, isTranslated)
EncodeUnicodeCkeckBox(isEncodeUnicode, onIsEncodeUnicodeChange)
}
LanguageList(targetLanguages)
}
}


@Composable
private fun EncodeUnicodeCkeckBox(
isEncodeUnicode: Boolean,
Expand Down Expand Up @@ -149,7 +122,8 @@ fun propertiesTranslateDialog(
project: Project?,
title: String,
file: VirtualFile,
onOKClick: (sourceLanguage: String, targetLanguages: List<String>, isEncodeUnicode: Boolean, sourceFileFormat: Charset) -> Unit,
onOKClick: (config: PropertiesTranslateConfig) -> Unit,
isShowOverwriteCheckBox: Boolean = false,
): PropertiesTranslateDialog {
val defaultSourceLanguage =
file.name.let { PropertiesUtil.getLanguageByName(it) }
Expand All @@ -162,10 +136,11 @@ fun propertiesTranslateDialog(
?: emptyList()

return PropertiesTranslateDialog(
project,
title,
defaultSourceLanguage,
onOKClick,
translatedLanguages
project = project,
title = title,
defaultSourceLanguage = defaultSourceLanguage,
isShowOverwriteCheckBox = isShowOverwriteCheckBox,
onOKClick = onOKClick,
translatedLanguages = translatedLanguages,
)
}
Loading

0 comments on commit 0eeb48c

Please sign in to comment.