Skip to content

Commit 309cb93

Browse files
authored
Merge pull request swiftlang#33661 from HassanElDesouky/localization-localize-diagnostics
[Localization] Create swift-def-to-yaml-converter tool
2 parents 502258e + fab497a commit 309cb93

File tree

11 files changed

+303
-8321
lines changed

11 files changed

+303
-8321
lines changed

include/swift/Localization/LocalizationFormat.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef SWIFT_LOCALIZATIONFORMAT_H
1818
#define SWIFT_LOCALIZATIONFORMAT_H
1919

20+
#include "llvm/ADT/ArrayRef.h"
2021
#include "llvm/ADT/Hashing.h"
2122
#include "llvm/ADT/Optional.h"
2223
#include "llvm/ADT/STLExtras.h"
@@ -42,6 +43,20 @@ namespace diag {
4243

4344
using namespace llvm::support;
4445

46+
class DefToYAMLConverter {
47+
llvm::ArrayRef<const char *> IDs;
48+
llvm::ArrayRef<const char *> Messages;
49+
50+
public:
51+
DefToYAMLConverter(llvm::ArrayRef<const char *> ids,
52+
llvm::ArrayRef<const char *> messages)
53+
: IDs(ids), Messages(messages) {
54+
assert(IDs.size() == Messages.size());
55+
}
56+
57+
void convert(llvm::raw_ostream &out);
58+
};
59+
4560
class LocalizationWriterInfo {
4661
public:
4762
using key_type = uint32_t;

lib/Localization/LocalizationFormat.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17+
#include "swift/Basic/Range.h"
1718
#include "swift/Localization/LocalizationFormat.h"
1819
#include "llvm/ADT/Optional.h"
1920
#include "llvm/ADT/SmallString.h"
@@ -190,5 +191,28 @@ operator>>(LocalizationInput &yin, T &diagnostics) {
190191
return yin;
191192
}
192193

194+
void DefToYAMLConverter::convert(llvm::raw_ostream &out) {
195+
for (auto i : swift::indices(IDs)) {
196+
out << "- id: " << IDs[i] << "\n";
197+
198+
const std::string &msg = Messages[i];
199+
200+
out << " msg: \"";
201+
// Add an escape character before a double quote `"` or a backslash `\`.
202+
for (unsigned j = 0; j < msg.length(); ++j) {
203+
if (msg[j] == '"') {
204+
out << '\\';
205+
out << '"';
206+
} else if (msg[j] == '\\') {
207+
out << '\\';
208+
out << '\\';
209+
} else {
210+
out << msg[j];
211+
}
212+
}
213+
out << "\"\r\n";
214+
}
215+
}
216+
193217
} // namespace diag
194218
} // namespace swift

localization/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ add_custom_target(diagnostic-database)
33
add_custom_command(TARGET diagnostic-database
44
COMMAND
55
${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/diagnostics/ ${CMAKE_BINARY_DIR}/share/swift/diagnostics/
6+
COMMAND
7+
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swift-def-to-yaml-converter"
8+
--output-directory ${CMAKE_BINARY_DIR}/share/swift/diagnostics/
69
COMMAND
710
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swift-serialize-diagnostics"
811
--input-file-path ${CMAKE_BINARY_DIR}/share/swift/diagnostics/en.yaml
@@ -11,6 +14,7 @@ add_custom_command(TARGET diagnostic-database
1114

1215
add_dependencies(swift-frontend diagnostic-database)
1316
add_dependencies(diagnostic-database swift-serialize-diagnostics)
17+
add_dependencies(diagnostic-database swift-def-to-yaml-converter)
1418

1519
swift_install_in_component(
1620
DIRECTORY ${CMAKE_BINARY_DIR}/share/swift/diagnostics/

localization/diagnostics/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)