Skip to content

Commit 216390f

Browse files
committed
feat: allow switching locale using a registry option
1 parent c57a18b commit 216390f

5 files changed

Lines changed: 52 additions & 1 deletion

File tree

src/main/kotlin/com/github/lppedd/cc/CC.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public object CC {
2323
}
2424

2525
public object Registry {
26+
public const val Locale: String = "com.github.lppedd.cc.locale"
2627
public const val VcsEnabled: String = "com.github.lppedd.cc.providers.vcs"
2728
}
2829

src/main/kotlin/com/github/lppedd/cc/CCBundle.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public object CCBundle {
1414
private const val BUNDLE = "messages.ConventionalCommitBundle"
1515

1616
private var bundleReference: Reference<ResourceBundle>? = null
17+
private var bundleLocale: Locale? = null
1718
private val bundle: ResourceBundle
1819
get() = derefBundle()
1920

@@ -25,11 +26,17 @@ public object CCBundle {
2526
public fun getOrDefault(@PropertyKey(resourceBundle = BUNDLE) key: String, defaultValue: String, vararg params: Any): String =
2627
BundleBase.messageOrDefault(bundle, key, defaultValue, *params)
2728

29+
@JvmStatic
30+
public fun setLocale(locale: Locale) {
31+
bundleReference = null
32+
bundleLocale = locale
33+
}
34+
2835
private fun derefBundle(): ResourceBundle {
2936
var ref = SoftReference.dereference(bundleReference)
3037

3138
if (ref == null) {
32-
ref = ResourceBundle.getBundle(BUNDLE)
39+
ref = ResourceBundle.getBundle(BUNDLE, bundleLocale ?: CCRegistry.getLocale())
3340
bundleReference = JavaSoftReference(ref)
3441
}
3542

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.lppedd.cc
2+
3+
import com.intellij.openapi.util.registry.Registry
4+
import java.util.*
5+
6+
/**
7+
* @author Edoardo Luppi
8+
*/
9+
internal object CCRegistry {
10+
/**
11+
* Returns the locale used by the plugin UI.
12+
*/
13+
fun getLocale(): Locale {
14+
val value = Registry.get(CC.Registry.Locale)
15+
return when (value.selectedOption) {
16+
"English (en_US)" -> Locale.ENGLISH
17+
"Chinese (zh_CN)" -> Locale.SIMPLIFIED_CHINESE
18+
else -> Locale.getDefault()
19+
}
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.lppedd.cc
2+
3+
import com.intellij.openapi.util.registry.RegistryValue
4+
import com.intellij.openapi.util.registry.RegistryValueListener
5+
6+
/**
7+
* @author Edoardo Luppi
8+
*/
9+
internal class CCRegistryValueListener : RegistryValueListener {
10+
override fun afterValueChanged(value: RegistryValue) {
11+
if (value.key == CC.Registry.Locale) {
12+
val locale = CCRegistry.getLocale()
13+
CCBundle.setLocale(locale)
14+
}
15+
}
16+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
<applicationListeners>
2020
<listener topic="com.intellij.openapi.project.ProjectManagerListener" class="com.github.lppedd.cc.CCProjectManagerListener" />
21+
<listener topic="com.intellij.openapi.util.registry.RegistryValueListener" class="com.github.lppedd.cc.CCRegistryValueListener" />
2122
</applicationListeners>
2223

2324
<extensions defaultExtensionNs="com.intellij">
@@ -99,6 +100,11 @@
99100
key="cc.notification.group.name"
100101
/>
101102

103+
<registryKey
104+
key="com.github.lppedd.cc.locale"
105+
description="Set the UI locale"
106+
defaultValue="[Default*|English (en_US)|Chinese (zh_CN)]"
107+
/>
102108
<registryKey
103109
key="com.github.lppedd.cc.providers.vcs"
104110
description="Enable/disable the new VCS Provider"

0 commit comments

Comments
 (0)