Skip to content

Commit 232ecad

Browse files
committed
本地化语言支持
1 parent ff3e11d commit 232ecad

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

CodeFormatServer/src/LanguageService.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include "CodeFormatServer/LanguageService.h"
2+
#include <fstream>
3+
#include <sstream>
4+
#include "nlohmann/json.hpp"
25
#include "CodeFormatServer/VSCode.h"
36
#include "CodeService/LuaCodeStyleOptions.h"
47
#include "CodeService/LuaFormatter.h"
@@ -64,6 +67,33 @@ std::shared_ptr<vscode::InitializeResult> LanguageService::OnInitialize(std::sha
6467
LanguageClient::GetInstance().UpdateOptions(configFile.workspace, configFile.path);
6568
}
6669

70+
std::filesystem::path localePath = param->initializationOptions.localeRoot;
71+
localePath /= param->locale + ".json";
72+
73+
if (std::filesystem::exists(localePath) && std::filesystem::is_regular_file(localePath))
74+
{
75+
std::fstream fin(localePath.string(), std::ios::in);
76+
77+
if (fin.is_open())
78+
{
79+
std::stringstream s;
80+
s << fin.rdbuf();
81+
std::string jsonText = s.str();
82+
auto json = nlohmann::json::parse(jsonText);
83+
84+
if (json.is_object())
85+
{
86+
std::map<std::string, std::string> languageMap;
87+
for (auto [key,value] : json.items())
88+
{
89+
languageMap.insert({key, value});
90+
}
91+
92+
LanguageTranslator::GetInstance().SetLanguageMap(std::move(languageMap));
93+
}
94+
}
95+
}
96+
6797

6898
return result;
6999
}
@@ -194,7 +224,7 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnTypeFormatting(
194224
auto position = param->position;
195225

196226
auto result = std::make_shared<vscode::DocumentFormattingResult>();
197-
if(parser->IsEmptyLine(position.line - 1))
227+
if (parser->IsEmptyLine(position.line - 1))
198228
{
199229
result->hasError = true;
200230
return result;

CodeFormatServer/src/VSCode.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ void vscode::InitializationOptions::Deserialize(nlohmann::json json)
224224
workspaceFolders.emplace_back(workspaceUri);
225225
}
226226
}
227+
if(json["localeRoot"].is_string())
228+
{
229+
localeRoot = json["localeRoot"];
230+
}
227231
}
228232

229233
void vscode::InitializeParams::Deserialize(nlohmann::json json)

CodeService/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,8 @@ target_sources(CodeService
6060
${CodeService_SOURCE_DIR}/src/LanguageTranslator.cpp
6161
)
6262

63+
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
64+
target_compile_options(CodeService PUBLIC /utf-8)
65+
endif ()
66+
6367
target_link_libraries(CodeService LuaParser)

include/CodeFormatServer/VSCode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class InitializationOptions : public Serializable
155155
public:
156156
std::vector<std::string> workspaceFolders;
157157
std::vector<EditorConfigSource> configFiles;
158-
158+
std::string localeRoot;
159159
void Deserialize(nlohmann::json json) override;
160160
};
161161

include/CodeService/LanguageTranslator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class LanguageTranslator
1313
std::string Get(const std::string& source);
1414

1515
void SetLanguageMap(std::map<std::string, std::string>&& languageDictionary);
16+
1617
private:
1718
std::map<std::string, std::string> _languageDictionary;
1819
};

0 commit comments

Comments
 (0)