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;
...
@@ -3,7 +3,6 @@ package emu.grasscutter.utils;
import
com.google.gson.JsonElement
;
import
com.google.gson.JsonElement
;
import
com.google.gson.JsonObject
;
import
com.google.gson.JsonObject
;
import
emu.grasscutter.Grasscutter
;
import
emu.grasscutter.Grasscutter
;
import
emu.grasscutter.game.Account
;
import
emu.grasscutter.game.player.Player
;
import
emu.grasscutter.game.player.Player
;
import
javax.annotation.Nullable
;
import
javax.annotation.Nullable
;
...
@@ -12,10 +11,11 @@ import java.util.concurrent.ConcurrentHashMap;
...
@@ -12,10 +11,11 @@ import java.util.concurrent.ConcurrentHashMap;
import
java.util.Map
;
import
java.util.Map
;
public
final
class
Language
{
public
final
class
Language
{
private
static
final
Map
<
String
,
Language
>
cachedLanguages
=
new
ConcurrentHashMap
<>();
private
final
JsonObject
languageData
;
private
final
JsonObject
languageData
;
private
final
String
languageCode
;
private
final
String
languageCode
;
private
final
Map
<
String
,
String
>
cachedTranslations
=
new
ConcurrentHashMap
<>();
private
final
Map
<
String
,
String
>
cachedTranslations
=
new
ConcurrentHashMap
<>();
private
static
final
Map
<
String
,
Language
>
cachedLanguages
=
new
ConcurrentHashMap
<>();
/**
/**
* Creates a language instance from a code.
* Creates a language instance from a code.
...
@@ -28,16 +28,14 @@ public final class Language {
...
@@ -28,16 +28,14 @@ public final class Language {
}
}
var
fallbackLanguageCode
=
Utils
.
getLanguageCode
(
Grasscutter
.
getConfig
().
DefaultLanguage
);
var
fallbackLanguageCode
=
Utils
.
getLanguageCode
(
Grasscutter
.
getConfig
().
DefaultLanguage
);
var
descript
er
=
getLanguageFileStreamDescripter
(
langCode
,
fallbackLanguageCode
);
var
descript
ion
=
getLanguageFileStreamDescripter
(
langCode
,
fallbackLanguageCode
);
var
actualLanguageCode
=
descript
er
.
getLanguageCode
();
var
actualLanguageCode
=
descript
ion
.
getLanguageCode
();
Language
languageInst
=
null
;
Language
languageInst
;
if
(
description
.
getLanguageFile
()
!=
null
)
{
if
(
descripter
.
getLanguageFile
()
!=
null
)
{
languageInst
=
new
Language
(
description
);
languageInst
=
new
Language
(
descripter
);
cachedLanguages
.
put
(
actualLanguageCode
,
languageInst
);
cachedLanguages
.
put
(
actualLanguageCode
,
languageInst
);
}
}
else
{
else
{
languageInst
=
cachedLanguages
.
get
(
actualLanguageCode
);
languageInst
=
cachedLanguages
.
get
(
actualLanguageCode
);
cachedLanguages
.
put
(
langCode
,
languageInst
);
cachedLanguages
.
put
(
langCode
,
languageInst
);
}
}
...
@@ -95,39 +93,41 @@ public final class Language {
...
@@ -95,39 +93,41 @@ public final class Language {
/**
/**
* Reads a file and creates a language instance.
* 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
;
@Nullable
JsonObject
languageData
=
null
;
languageCode
=
descript
er
.
getLanguageCode
();
languageCode
=
descript
ion
.
getLanguageCode
();
try
{
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
)
{
}
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
;
this
.
languageData
=
languageData
;
}
}
/**
/**
* create a
Internal
Language
File
StreamDescript
er
* create a LanguageStreamDescript
ion
* @param languageCode The name of the language code.
* @param languageCode The name of the language code.
* @param fallbackLanguageCode The name of the fallback 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
fileName
=
languageCode
+
".json"
;
var
fallback
=
fallbackLanguageCode
+
".json"
;
var
fallback
=
fallbackLanguageCode
+
".json"
;
String
actualLanguageCode
=
languageCode
;
String
actualLanguageCode
=
languageCode
;
if
(
cachedLanguages
.
containsKey
(
actualLanguageCode
))
{
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
);
InputStream
file
=
Grasscutter
.
class
.
getResourceAsStream
(
"/languages/"
+
fileName
);
if
(
file
==
null
)
{
// Provided fallback language.
if
(
file
==
null
)
{
// Provided fallback language.
actualLanguageCode
=
fallbackLanguageCode
;
actualLanguageCode
=
fallbackLanguageCode
;
if
(
cachedLanguages
.
containsKey
(
actualLanguageCode
))
{
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
);
file
=
Grasscutter
.
class
.
getResourceAsStream
(
"/languages/"
+
fallback
);
Grasscutter
.
getLogger
().
warn
(
"Failed to load language file: "
+
fileName
+
", falling back to: "
+
fallback
);
Grasscutter
.
getLogger
().
warn
(
"Failed to load language file: "
+
fileName
+
", falling back to: "
+
fallback
);
}
}
...
@@ -135,8 +135,9 @@ public final class Language {
...
@@ -135,8 +135,9 @@ public final class Language {
if
(
file
==
null
)
{
// Fallback the fallback language.
if
(
file
==
null
)
{
// Fallback the fallback language.
actualLanguageCode
=
"en-US"
;
actualLanguageCode
=
"en-US"
;
if
(
cachedLanguages
.
containsKey
(
actualLanguageCode
))
{
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"
);
file
=
Grasscutter
.
class
.
getResourceAsStream
(
"/languages/en-US.json"
);
Grasscutter
.
getLogger
().
warn
(
"Failed to load language file: "
+
fallback
+
", falling back to: 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 {
...
@@ -144,7 +145,7 @@ public final class Language {
if
(
file
==
null
)
if
(
file
==
null
)
throw
new
RuntimeException
(
"Unable to load the primary, fallback, and 'en-US' language files."
);
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 {
...
@@ -180,11 +181,11 @@ public final class Language {
this
.
cachedTranslations
.
put
(
key
,
result
);
return
result
;
this
.
cachedTranslations
.
put
(
key
,
result
);
return
result
;
}
}
private
static
class
Internal
Language
File
StreamDescript
er
{
private
static
class
LanguageStreamDescript
ion
{
private
String
languageCode
;
private
final
String
languageCode
;
private
InputStream
languageFile
;
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
.
languageCode
=
languageCode
;
this
.
languageFile
=
languageFile
;
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