Skip to content

[Localization] Switch diagnostics/localization to .strings format #60707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 17 additions & 41 deletions include/swift/Localization/LocalizationFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/OnDiskHashTable.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/YAMLParser.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdint>
#include <memory>
Expand All @@ -50,14 +48,14 @@ enum LocalizationProducerState : uint8_t {
FailedInitialization
};

class DefToYAMLConverter {
class DefToStringsConverter {
llvm::ArrayRef<const char *> IDs;
llvm::ArrayRef<const char *> Messages;

public:
DefToYAMLConverter(llvm::ArrayRef<const char *> ids,
llvm::ArrayRef<const char *> messages)
: IDs(ids), Messages(messages) {
DefToStringsConverter(llvm::ArrayRef<const char *> ids,
llvm::ArrayRef<const char *> messages)
: IDs(ids), Messages(messages) {
assert(IDs.size() == Messages.size());
}

Expand Down Expand Up @@ -181,9 +179,9 @@ class LocalizationProducer {
llvm::StringRef defaultMessage);

/// \returns a `SerializedLocalizationProducer` pointer if the serialized
/// diagnostics file available, otherwise returns a `YAMLLocalizationProducer`
/// if the `YAML` file is available. If both files aren't available returns a
/// `nullptr`.
/// diagnostics file available, otherwise returns a
/// `StringsLocalizationProducer` if the `.strings` file is available. If both
/// files aren't available returns a `nullptr`.
static std::unique_ptr<LocalizationProducer>
producerFor(llvm::StringRef locale, llvm::StringRef path,
bool printDiagnosticNames);
Expand All @@ -204,15 +202,15 @@ class LocalizationProducer {
virtual llvm::StringRef getMessage(swift::DiagID id) const = 0;
};

class YAMLLocalizationProducer final : public LocalizationProducer {
std::vector<std::string> diagnostics;
class StringsLocalizationProducer final : public LocalizationProducer {
std::string filePath;

std::vector<std::string> diagnostics;

public:
/// The diagnostics IDs that are no longer available in `.def`
std::vector<std::string> unknownIDs;
explicit YAMLLocalizationProducer(llvm::StringRef filePath,
bool printDiagnosticNames = false);
explicit StringsLocalizationProducer(llvm::StringRef filePath,
bool printDiagnosticNames = false)
: LocalizationProducer(printDiagnosticNames), filePath(filePath) {}

/// Iterate over all of the available (non-empty) translations
/// maintained by this producer, callback gets each translation
Expand All @@ -223,6 +221,10 @@ class YAMLLocalizationProducer final : public LocalizationProducer {
protected:
bool initializeImpl() override;
llvm::StringRef getMessage(swift::DiagID id) const override;

private:
static void readStringsFile(llvm::MemoryBuffer *in,
std::vector<std::string> &diagnostics);
};

class SerializedLocalizationProducer final : public LocalizationProducer {
Expand All @@ -242,32 +244,6 @@ class SerializedLocalizationProducer final : public LocalizationProducer {
llvm::StringRef getMessage(swift::DiagID id) const override;
};

class LocalizationInput : public llvm::yaml::Input {
using Input::Input;

/// Read diagnostics in the YAML file iteratively
template <typename T, typename Context>
friend typename std::enable_if<llvm::yaml::has_SequenceTraits<T>::value,
void>::type
readYAML(llvm::yaml::IO &io, T &Seq, T &unknownIDs, bool, Context &Ctx);

template <typename T>
friend typename std::enable_if<llvm::yaml::has_SequenceTraits<T>::value,
LocalizationInput &>::type
operator>>(LocalizationInput &yin, T &diagnostics);

public:
/// A vector that keeps track of the diagnostics IDs that are available in
/// YAML and not available in `.def` files.
std::vector<std::string> unknownIDs;

/// A diagnostic ID might be present in YAML and not in `.def` file, if that's
/// the case the `id` won't have a `DiagID` value.
/// If the `id` is available in `.def` file this method will return the `id`'s
/// value, otherwise this method won't return a value.
static llvm::Optional<uint32_t> readID(llvm::yaml::IO &io);
};

} // namespace diag
} // namespace swift

Expand Down
2 changes: 2 additions & 0 deletions lib/ConstExtract/ConstExtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "swift/AST/TypeCheckRequests.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/YAMLParser.h"
#include "llvm/Support/YAMLTraits.h"

#include <set>
#include <sstream>
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
// for the specified locale code.
llvm::SmallString<128> localizationPath(A->getValue());
llvm::sys::path::append(localizationPath, Opts.LocalizationCode);
llvm::sys::path::replace_extension(localizationPath, ".yaml");
llvm::sys::path::replace_extension(localizationPath, ".strings");
if (!llvm::sys::fs::exists(localizationPath)) {
Diags.diagnose(SourceLoc(), diag::warning_cannot_find_locale_file,
Opts.LocalizationCode, localizationPath);
Expand Down
Loading