use Locale.default() for the initial language
pull/158/head
骏烈 7 years ago
parent b993cd5f67
commit bce928dc04

@ -28,6 +28,7 @@ import org.eclipse.swt.graphics.Image
import org.eclipse.swt.widgets.Display
import org.eclipse.ui.plugin.AbstractUIPlugin
import org.osgi.framework.BundleContext
import java.util.Locale
/**
* @author caikang
@ -72,20 +73,22 @@ class SmartfoxActivator : AbstractUIPlugin() {
return image!!
}
val locale: String get() {
val lang = preferenceStore.getString(localeKey)
return if (lang.isNullOrBlank()) {
"zh"
} else {
lang
val locale: String
get() {
val language = preferenceStore.getString(localeKey)
if (language.isNullOrBlank()) {
val lang = Locale.getDefault().language
return if (lang != Locale.ENGLISH.language && lang != Locale.CHINESE.language) {
Locale.CHINESE.language
} else Locale.getDefault().language
}
return language
}
}
fun toggleLocale() {
preferenceStore.setValue(localeKey, when (locale) {
"en" -> "zh"
else -> "en"
})
val lang = if (Locale.ENGLISH.language == locale) Locale.CHINESE.language else Locale.ENGLISH.language
preferenceStore.setValue(localeKey, lang)
}
fun getRule(rule: String): Rule {

@ -32,15 +32,12 @@ import com.intellij.openapi.project.DumbAware
* @date 2017/06/20
*/
class SwitchLanguageAction : AnAction(), DumbAware {
val p3cConfig = P3cConfig::class.java.getService()
private val p3cConfig = P3cConfig::class.java.getService()
val textKey = "com.alibaba.p3c.action.switch_language.text"
private val textKey = "com.alibaba.p3c.action.switch_language.text"
override fun actionPerformed(e: AnActionEvent) {
p3cConfig.locale = when (p3cConfig.locale) {
P3cConfig.localeZh -> P3cConfig.localeEn
else -> P3cConfig.localeZh
}
p3cConfig.toggleLanguage()
BalloonNotifications.showSuccessNotification(P3cBundle.getMessage("$textKey.success"), e.project,
NotificationListener { notification, _ ->
notification.expire()

@ -20,6 +20,7 @@ import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.StoragePathMacros
import com.intellij.util.xmlb.XmlSerializerUtil
import java.util.Locale
/**
*
@ -27,8 +28,7 @@ import com.intellij.util.xmlb.XmlSerializerUtil
* @author caikang
* @date 2017/06/19
*/
@State(name = "P3cConfig",
storages = arrayOf(Storage(file = "${StoragePathMacros.APP_CONFIG}/smartfox/p3c.xml")))
@State(name = "P3cConfig", storages = arrayOf(Storage(file = "${StoragePathMacros.APP_CONFIG}/smartfox/p3c.xml")))
class P3cConfig : PersistentStateComponent<P3cConfig> {
var astCacheTime = 1000L
var astCacheEnable = true
@ -38,7 +38,21 @@ class P3cConfig : PersistentStateComponent<P3cConfig> {
var analysisBeforeCheckin = false
var locale = localeZh
var locale: String = ""
get() {
if (field.isEmpty()) {
val lang = Locale.getDefault().language
return if (lang != Locale.ENGLISH.language && lang != Locale.CHINESE.language) {
Locale.CHINESE.language
} else Locale.getDefault().language
}
return field
}
fun toggleLanguage() {
locale = if (localeEn == locale) localeZh else localeEn
}
override fun getState(): P3cConfig {
return this
@ -51,8 +65,9 @@ class P3cConfig : PersistentStateComponent<P3cConfig> {
XmlSerializerUtil.copyBean(state, this)
}
companion object {
val localeEn = "en"
val localeZh = "zh"
val localeEn = Locale.ENGLISH.language!!
val localeZh = Locale.CHINESE.language!!
}
}

@ -29,7 +29,7 @@ import java.util.ResourceBundle
* @date 2017/06/20
*/
object P3cBundle {
val p3cConfig = P3cConfig::class.java.getService()
private val p3cConfig = P3cConfig::class.java.getService()
private val resourceBundle = ResourceBundle.getBundle("messages.P3cBundle",
Locale(p3cConfig.locale), I18nResources.XmlControl())

Loading…
Cancel
Save