Skip to content

Commit

Permalink
Add a new resource bundle for translations (#150)
Browse files Browse the repository at this point in the history
New strings will automatically be exported and imported for translation.
  • Loading branch information
hopskipnfall authored Feb 4, 2025
1 parent a69dbed commit a615a4d
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 14 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/crowdin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Crowdin

on:
push:
branches: [ master ]

jobs:
synchronize-with-crowdin:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: crowdin action
uses: crowdin/github-action@v2
with:
upload_sources: true
upload_translations: false
download_translations: true
localization_branch_name: l10n_crowdin_translations
create_pull_request: true
pull_request_title: 'New Crowdin Translations'
pull_request_body: 'New Crowdin translations by [Crowdin GH Action](https://github.com/crowdin/github-action)'
pull_request_base_branch_name: 'master'
env:
# A classic GitHub Personal Access Token with the 'repo' scope selected (the user should have write access to the repository).
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# A numeric ID, found at https://crowdin.com/project/<projectName>/tools/api
CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}

# Visit https://crowdin.com/settings#api-key to create this token
CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}
12 changes: 12 additions & 0 deletions crowdin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"project_id_env": "CROWDIN_PROJECT_ID"
"api_token_env": "CROWDIN_PERSONAL_TOKEN"
"base_path": "."

"preserve_hierarchy": true

"files": [
{
"source": "emulinker/src/main/i18n/messages.properties",
"translation": "emulinker/src/main/i18n/messages_%two_letters_code%.properties"
}
]
2 changes: 1 addition & 1 deletion emulinker/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ sourceSets {
kotlin.srcDir("src/main/java")
kotlin.srcDir("build/resources/main")

resources { srcDirs("conf") }
resources { srcDirs("conf", "src/main/i18n") }
}

test {
Expand Down
3 changes: 0 additions & 3 deletions emulinker/conf/language.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ KailleraServerImpl.LoginMessage.3=Edit language.properties to setup your login a

KailleraServerImpl.JoinGameMessage.1=Message that appears when a user joins/starts a game!

# Other Announcements
KailleraServerImpl.UserCreatedGameAnnouncement={0} created game: {1}

# Server Denied Messages
KailleraServerImpl.LoginDeniedPingTooHigh=Ping Too High: {0}
KailleraServerImpl.LoginDeniedServerFull=Server is full\!
Expand Down
2 changes: 2 additions & 0 deletions emulinker/src/main/i18n/messages.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
! 0 is the username and 1 is the name of the game.
KailleraServerImpl.UserCreatedGameAnnouncement={0} created game: {1}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.emulinker.kaillera.model.event.GameChatEvent
import org.emulinker.kaillera.model.exception.ActionException
import org.emulinker.kaillera.model.exception.GameChatException
import org.emulinker.kaillera.model.impl.KailleraGameImpl
import org.emulinker.util.EmuLang
import org.emulinker.util.EmuLang.getStringOrNull
import org.emulinker.util.EmuUtil.min
import org.emulinker.util.EmuUtil.threadSleep
import org.emulinker.util.EmuUtil.toMillisDouble
Expand Down Expand Up @@ -453,10 +453,7 @@ class GameChatAction(
} else if (message.message == "/stop") {
if (lookingForGameReporter.cancelActionsForUser(clientHandler.user.id)) {
game.announce(
EmuLang.getStringOrDefault(
"KailleraServerImpl.CanceledPendingTweet",
default = "Canceled pending tweet.",
),
getStringOrNull("KailleraServerImpl.CanceledPendingTweet") ?: "Canceled pending tweet.",
clientHandler.user,
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.util.concurrent.ConcurrentMap
import kotlin.time.Duration.Companion.seconds
import org.emulinker.config.RuntimeFlags
import org.emulinker.kaillera.model.KailleraUser
import org.emulinker.util.EmuLang
import org.emulinker.util.EmuLang.getStringOrNull
import org.emulinker.util.TaskScheduler
import org.koin.core.component.KoinComponent
import org.koin.core.component.get
Expand Down Expand Up @@ -118,7 +118,7 @@ class TwitterBroadcaster(
twitter.postTweet(
TweetParameters.builder()
.reply(TweetParameters.Reply.builder().inReplyToTweetId(tweetId).build())
.text(EmuLang.getStringOrDefault("KailleraServerImpl.TweetCloseMessage", ""))
.text(getStringOrNull("KailleraServerImpl.TweetCloseMessage") ?: "")
.build()
)
logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import org.emulinker.kaillera.release.ReleaseInfo
import org.emulinker.util.EmuLang
import org.emulinker.util.EmuUtil
import org.emulinker.util.EmuUtil.threadSleep
import org.emulinker.util.NewEmuLang
import org.emulinker.util.TaskScheduler
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
Expand Down Expand Up @@ -674,7 +675,11 @@ class KailleraServer(
.log("Caught exception while making owner join game! This shouldn't happen!")
}
announce(
EmuLang.getString("KailleraServerImpl.UserCreatedGameAnnouncement", user.name, game.romName),
NewEmuLang.getString(
"KailleraServerImpl.UserCreatedGameAnnouncement",
user.name!!,
game.romName,
),
false,
)
if (
Expand Down
4 changes: 2 additions & 2 deletions emulinker/src/main/java/org/emulinker/util/EmuLang.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ object EmuLang {
return false
}

fun getStringOrDefault(key: String, default: String): String =
fun getStringOrNull(key: String): String? =
try {
RESOURCE_BUNDLE.getString(key)
} catch (e: MissingResourceException) {
default
null
}

fun getString(key: String): String =
Expand Down
53 changes: 53 additions & 0 deletions emulinker/src/main/java/org/emulinker/util/NewEmuLang.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.emulinker.util

import com.google.common.flogger.FluentLogger
import java.lang.Exception
import java.text.MessageFormat
import java.util.Locale
import java.util.ResourceBundle

object NewEmuLang {
private const val BUNDLE_NAME = "messages"

private val RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME, Locale.ENGLISH)

private val logger = FluentLogger.forEnclosingClass()

/*
public static void reload()
{
try
{
Class klass = RESOURCE_BUNDLE.getClass().getSuperclass();
Field field = klass.getDeclaredField("cacheList");
field.setAccessible(true);
sun.misc.SoftCache cache = (sun.misc.SoftCache)field.get(null);
cache.clear();
}
catch(Exception e)
{
}
}
*/

fun hasString(key: String): Boolean {
if (RESOURCE_BUNDLE.containsKey(key)) {
try {
RESOURCE_BUNDLE.getString(key)
return true
} catch (e: Exception) {
// It exists but is not readable.
e.printStackTrace()
}
}
return false
}

fun getString(key: String): String = RESOURCE_BUNDLE.getString(key)

fun getString(key: String, vararg messageArgs: Any): String {
val str = RESOURCE_BUNDLE.getString(key)
return MessageFormat(str).format(messageArgs)
}
}

0 comments on commit a615a4d

Please sign in to comment.