Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
ziqian zhang
Grasscutter
Commits
5d49323c
Commit
5d49323c
authored
May 10, 2022
by
KingRainbow44
Browse files
my poor, poor, language system (Formatting refactor)
parent
a26afe7d
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/utils/Language.java
View file @
5d49323c
...
...
@@ -3,7 +3,6 @@ package emu.grasscutter.utils;
import
com.google.gson.JsonElement
;
import
com.google.gson.JsonObject
;
import
emu.grasscutter.Grasscutter
;
import
emu.grasscutter.game.Account
;
import
emu.grasscutter.game.player.Player
;
import
javax.annotation.Nullable
;
...
...
@@ -12,10 +11,11 @@ import java.util.concurrent.ConcurrentHashMap;
import
java.util.Map
;
public
final
class
Language
{
private
static
final
Map
<
String
,
Language
>
cachedLanguages
=
new
ConcurrentHashMap
<>();
private
final
JsonObject
languageData
;
private
final
String
languageCode
;
private
final
Map
<
String
,
String
>
cachedTranslations
=
new
ConcurrentHashMap
<>();
private
static
final
Map
<
String
,
Language
>
cachedLanguages
=
new
ConcurrentHashMap
<>();
/**
* Creates a language instance from a code.
...
...
@@ -28,16 +28,14 @@ public final class Language {
}
var
fallbackLanguageCode
=
Utils
.
getLanguageCode
(
Grasscutter
.
getConfig
().
DefaultLanguage
);
var
descript
er
=
getLanguageFileStreamDescripter
(
langCode
,
fallbackLanguageCode
);
var
actualLanguageCode
=
descript
er
.
getLanguageCode
();
var
descript
ion
=
getLanguageFileStreamDescripter
(
langCode
,
fallbackLanguageCode
);
var
actualLanguageCode
=
descript
ion
.
getLanguageCode
();
Language
languageInst
=
null
;
if
(
descripter
.
getLanguageFile
()
!=
null
)
{
languageInst
=
new
Language
(
descripter
);
Language
languageInst
;
if
(
description
.
getLanguageFile
()
!=
null
)
{
languageInst
=
new
Language
(
description
);
cachedLanguages
.
put
(
actualLanguageCode
,
languageInst
);
}
else
{
}
else
{
languageInst
=
cachedLanguages
.
get
(
actualLanguageCode
);
cachedLanguages
.
put
(
langCode
,
languageInst
);
}
...
...
@@ -95,39 +93,41 @@ public final class Language {
/**
* Reads a file and creates a language instance.
*/
private
Language
(
Internal
Language
File
StreamDescript
er
descript
er
)
{
private
Language
(
LanguageStreamDescript
ion
descript
ion
)
{
@Nullable
JsonObject
languageData
=
null
;
languageCode
=
descript
er
.
getLanguageCode
();
languageCode
=
descript
ion
.
getLanguageCode
();
try
{
languageData
=
Grasscutter
.
getGsonFactory
().
fromJson
(
Utils
.
readFromInputStream
(
descript
er
.
getLanguageFile
()),
JsonObject
.
class
);
languageData
=
Grasscutter
.
getGsonFactory
().
fromJson
(
Utils
.
readFromInputStream
(
descript
ion
.
getLanguageFile
()),
JsonObject
.
class
);
}
catch
(
Exception
exception
)
{
Grasscutter
.
getLogger
().
warn
(
"Failed to load language file: "
+
descript
er
.
getLanguageCode
(),
exception
);
Grasscutter
.
getLogger
().
warn
(
"Failed to load language file: "
+
descript
ion
.
getLanguageCode
(),
exception
);
}
this
.
languageData
=
languageData
;
}
/**
* create a
Internal
Language
File
StreamDescript
er
* create a LanguageStreamDescript
ion
* @param languageCode The name of the language code.
* @param fallbackLanguageCode The name of the fallback language code.
*/
private
static
Internal
Language
File
StreamDescript
er
getLanguageFileStreamDescripter
(
String
languageCode
,
String
fallbackLanguageCode
)
{
private
static
LanguageStreamDescript
ion
getLanguageFileStreamDescripter
(
String
languageCode
,
String
fallbackLanguageCode
)
{
var
fileName
=
languageCode
+
".json"
;
var
fallback
=
fallbackLanguageCode
+
".json"
;
String
actualLanguageCode
=
languageCode
;
if
(
cachedLanguages
.
containsKey
(
actualLanguageCode
))
{
return
new
Internal
Language
File
StreamDescript
er
(
actualLanguageCode
,
null
);
return
new
LanguageStreamDescript
ion
(
actualLanguageCode
,
null
);
}
InputStream
file
=
Grasscutter
.
class
.
getResourceAsStream
(
"/languages/"
+
fileName
);
if
(
file
==
null
)
{
// Provided fallback language.
actualLanguageCode
=
fallbackLanguageCode
;
if
(
cachedLanguages
.
containsKey
(
actualLanguageCode
))
{
return
new
Internal
Language
File
StreamDescript
er
(
actualLanguageCode
,
null
);
return
new
LanguageStreamDescript
ion
(
actualLanguageCode
,
null
);
}
file
=
Grasscutter
.
class
.
getResourceAsStream
(
"/languages/"
+
fallback
);
Grasscutter
.
getLogger
().
warn
(
"Failed to load language file: "
+
fileName
+
", falling back to: "
+
fallback
);
}
...
...
@@ -135,8 +135,9 @@ public final class Language {
if
(
file
==
null
)
{
// Fallback the fallback language.
actualLanguageCode
=
"en-US"
;
if
(
cachedLanguages
.
containsKey
(
actualLanguageCode
))
{
return
new
Internal
Language
File
StreamDescript
er
(
actualLanguageCode
,
null
);
return
new
LanguageStreamDescript
ion
(
actualLanguageCode
,
null
);
}
file
=
Grasscutter
.
class
.
getResourceAsStream
(
"/languages/en-US.json"
);
Grasscutter
.
getLogger
().
warn
(
"Failed to load language file: "
+
fallback
+
", falling back to: en-US.json"
);
}
...
...
@@ -144,7 +145,7 @@ public final class Language {
if
(
file
==
null
)
throw
new
RuntimeException
(
"Unable to load the primary, fallback, and 'en-US' language files."
);
return
new
Internal
Language
File
StreamDescript
er
(
actualLanguageCode
,
file
);
return
new
LanguageStreamDescript
ion
(
actualLanguageCode
,
file
);
}
/**
...
...
@@ -180,11 +181,11 @@ public final class Language {
this
.
cachedTranslations
.
put
(
key
,
result
);
return
result
;
}
private
static
class
Internal
Language
File
StreamDescript
er
{
private
String
languageCode
;
private
InputStream
languageFile
;
private
static
class
LanguageStreamDescript
ion
{
private
final
String
languageCode
;
private
final
InputStream
languageFile
;
public
Internal
Language
File
StreamDescript
er
(
String
languageCode
,
InputStream
languageFile
)
{
public
LanguageStreamDescript
ion
(
String
languageCode
,
InputStream
languageFile
)
{
this
.
languageCode
=
languageCode
;
this
.
languageFile
=
languageFile
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment