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
57cc1113
Commit
57cc1113
authored
May 07, 2022
by
KingRainbow44
Browse files
Simplify the language fallback system
parent
730c9938
Changes
4
Show whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/Config.java
View file @
57cc1113
...
...
@@ -23,7 +23,7 @@ public final class Config {
public
GameServerOptions
GameServer
=
new
GameServerOptions
();
public
DispatchServerOptions
DispatchServer
=
new
DispatchServerOptions
();
public
Locale
LocaleLanguage
=
Locale
.
getDefault
();
public
Locale
DefaultLanguage
=
Locale
.
US
;
public
Locale
DefaultLanguage
=
Locale
.
ENGLISH
;
public
Boolean
OpenStamina
=
true
;
public
GameServerOptions
getGameServerOptions
()
{
...
...
src/main/java/emu/grasscutter/Grasscutter.java
View file @
57cc1113
...
...
@@ -149,9 +149,10 @@ public final class Grasscutter {
public
static
void
loadLanguage
()
{
var
locale
=
config
.
LocaleLanguage
;
String
languageTag
=
locale
.
toLanguageTag
();
var
languageTag
=
locale
.
toLanguageTag
();
if
(
languageTag
.
equals
(
"und"
))
{
Grasscutter
.
getLogger
().
error
(
"Illegal locale language, using en-US instead."
);
Grasscutter
.
getLogger
().
error
(
"Illegal locale language, using
'
en-US
'
instead."
);
language
=
Language
.
getLanguage
(
"en-US"
);
}
else
{
language
=
Language
.
getLanguage
(
languageTag
);
...
...
src/main/java/emu/grasscutter/utils/Language.java
View file @
57cc1113
...
...
@@ -19,7 +19,7 @@ public final class Language {
* @return A language instance.
*/
public
static
Language
getLanguage
(
String
langCode
)
{
return
new
Language
(
langCode
+
".json"
);
return
new
Language
(
langCode
+
".json"
,
Grasscutter
.
getConfig
().
DefaultLanguage
.
toLanguageTag
()
);
}
/**
...
...
@@ -30,6 +30,7 @@ public final class Language {
*/
public
static
String
translate
(
String
key
,
Object
...
args
)
{
String
translated
=
Grasscutter
.
getLanguage
().
get
(
key
);
try
{
return
translated
.
formatted
(
args
);
}
catch
(
Exception
exception
)
{
...
...
@@ -38,48 +39,25 @@ public final class Language {
}
}
/**
* creates a language instance.
* @param fileName The name of the language file.
*/
private
Language
(
String
fileName
)
{
@Nullable
JsonObject
languageData
=
null
;
languageData
=
loadLanguage
(
fileName
);
if
(
languageData
==
null
)
{
Grasscutter
.
getLogger
().
info
(
"Now switch to default language"
);
languageData
=
loadDefaultLanguage
();
}
assert
languageData
!=
null
:
"languageData is null"
;
this
.
languageData
=
languageData
;
}
/**
* Load default language file and creates a language instance.
* @return language data
*/
private
JsonObject
loadDefaultLanguage
()
{
var
fileName
=
Grasscutter
.
getConfig
().
DefaultLanguage
.
toLanguageTag
()
+
".json"
;
return
loadLanguage
(
fileName
);
}
/**
* Reads a file and creates a language instance.
* @param fileName The name of the language file.
* @return language data
*/
private
JsonObject
load
Language
(
String
fileName
)
{
private
Language
(
String
fileName
,
String
fallback
)
{
@Nullable
JsonObject
languageData
=
null
;
try
{
InputStream
file
=
Grasscutter
.
class
.
getResourceAsStream
(
"/languages/"
+
fileName
);
if
(
file
==
null
)
{
file
=
Grasscutter
.
class
.
getResourceAsStream
(
"/languages/"
+
fallback
);
}
languageData
=
Grasscutter
.
getGsonFactory
().
fromJson
(
Utils
.
readFromInputStream
(
file
),
JsonObject
.
class
);
}
catch
(
Exception
exception
)
{
Grasscutter
.
getLogger
().
warn
(
"Failed to load language file: "
+
fileName
);
Grasscutter
.
getLogger
().
warn
(
"Failed to load language file: "
+
fileName
,
exception
);
}
return
languageData
;
this
.
languageData
=
languageData
;
}
/**
...
...
src/main/java/emu/grasscutter/utils/Utils.java
View file @
57cc1113
package
emu.grasscutter.utils
;
import
java.io.*
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.file.Files
;
import
java.nio.file.StandardCopyOption
;
import
java.time.*
;
...
...
@@ -254,7 +255,7 @@ public final class Utils {
*/
public
static
String
readFromInputStream
(
InputStream
stream
)
{
StringBuilder
stringBuilder
=
new
StringBuilder
();
try
(
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
stream
,
"UTF-8"
)))
{
try
(
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
stream
,
StandardCharsets
.
UTF_8
)))
{
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
stringBuilder
.
append
(
line
);
}
stream
.
close
();
...
...
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