Skip to content

Commit 9e85a39

Browse files
authored
Merge pull request #32630 from HassanElDesouky/LocaleFlagsRE
Reopen [Diag] Create frontend flags for localization
2 parents 0990ffe + 8ee34b7 commit 9e85a39

14 files changed

+361
-3
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,10 @@ namespace swift {
740740
}
741741

742742
void setLocalization(std::string locale, std::string path) {
743-
if (!locale.empty() && !path.empty())
744-
localization =
745-
std::make_unique<diag::YAMLLocalizationProducer>(locale, path);
743+
assert(!locale.empty());
744+
assert(!path.empty());
745+
localization =
746+
std::make_unique<diag::YAMLLocalizationProducer>(locale, path);
746747
}
747748

748749
void ignoreDiagnostic(DiagID id) {

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ ERROR(error_unknown_arg,none,
7676
"unknown argument: '%0'", (StringRef))
7777
ERROR(error_invalid_arg_value,none,
7878
"invalid value '%1' in '%0'", (StringRef, StringRef))
79+
WARNING(warning_invalid_locale_code,none,
80+
"unsupported locale code; supported locale codes are: '%0'", (StringRef))
81+
WARNING(warning_locale_path_not_found,none,
82+
"specified localization directory '%0' does not exist, "
83+
"translation is disabled", (StringRef))
84+
WARNING(warning_cannot_find_locale_file,none,
85+
"cannot find translations for '%0' at '%1': no such file", (StringRef, StringRef))
7986
WARNING(warning_cannot_multithread_batch_mode,none,
8087
"ignoring -num-threads argument; cannot multithread batch mode", ())
8188
ERROR(error_unsupported_option_argument,none,
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
//===--- LocalizationLanguages.def - Localization Languages ----*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file has all language codes defined by ISO 639-1 for localization.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SUPPORTED_LOCALE
18+
#define SUPPORTED_LOCALE(Code, Language)
19+
#endif
20+
21+
SUPPORTED_LOCALE(ab, Abkhazian)
22+
SUPPORTED_LOCALE(aa, Afar)
23+
SUPPORTED_LOCALE(af, Afrikaans)
24+
SUPPORTED_LOCALE(ak, Akan)
25+
SUPPORTED_LOCALE(sq, Albanian)
26+
SUPPORTED_LOCALE(am, Amharic)
27+
SUPPORTED_LOCALE(ar, Arabic)
28+
SUPPORTED_LOCALE(an, Aragonese)
29+
SUPPORTED_LOCALE(hy, Armenian)
30+
SUPPORTED_LOCALE(as, Assamese)
31+
SUPPORTED_LOCALE(av, Avaric)
32+
SUPPORTED_LOCALE(ae, Avestan)
33+
SUPPORTED_LOCALE(ay, Aymara)
34+
SUPPORTED_LOCALE(az, Azerbaijani)
35+
SUPPORTED_LOCALE(bm, Bambara)
36+
SUPPORTED_LOCALE(ba, Bashkir)
37+
SUPPORTED_LOCALE(eu, Basque)
38+
SUPPORTED_LOCALE(be, Belarusian)
39+
SUPPORTED_LOCALE(bn, Bengali)
40+
SUPPORTED_LOCALE(bh, Bihari languages)
41+
SUPPORTED_LOCALE(bi, Bislama)
42+
SUPPORTED_LOCALE(bs, Bosnian)
43+
SUPPORTED_LOCALE(br, Breton)
44+
SUPPORTED_LOCALE(bg, Bulgarian)
45+
SUPPORTED_LOCALE(my, Burmese)
46+
SUPPORTED_LOCALE(ca, Catalan)
47+
SUPPORTED_LOCALE(km, Central Khmer)
48+
SUPPORTED_LOCALE(ch, Chamorro)
49+
SUPPORTED_LOCALE(ce, Chechen)
50+
SUPPORTED_LOCALE(ny, Chichewa)
51+
SUPPORTED_LOCALE(zh, Chinese)
52+
SUPPORTED_LOCALE(cu, Church Slavonic)
53+
SUPPORTED_LOCALE(cv, Chuvash)
54+
SUPPORTED_LOCALE(kw, Cornish)
55+
SUPPORTED_LOCALE(co, Corsican)
56+
SUPPORTED_LOCALE(cr, Cree)
57+
SUPPORTED_LOCALE(hr, Croatian)
58+
SUPPORTED_LOCALE(cs, Czech)
59+
SUPPORTED_LOCALE(da, Danish)
60+
SUPPORTED_LOCALE(dv, Divehi)
61+
SUPPORTED_LOCALE(nl, Dutch)
62+
SUPPORTED_LOCALE(dz, Dzongkha)
63+
SUPPORTED_LOCALE(en, English)
64+
SUPPORTED_LOCALE(eo, Esperanto)
65+
SUPPORTED_LOCALE(et, Estonian)
66+
SUPPORTED_LOCALE(ee, Ewe)
67+
SUPPORTED_LOCALE(fo, Faroese)
68+
SUPPORTED_LOCALE(fj, Fijian)
69+
SUPPORTED_LOCALE(fi, Finnish)
70+
SUPPORTED_LOCALE(fr, French)
71+
SUPPORTED_LOCALE(ff, Fulah)
72+
SUPPORTED_LOCALE(gd, Gaelic)
73+
SUPPORTED_LOCALE(gl, Galician)
74+
SUPPORTED_LOCALE(lg, Ganda)
75+
SUPPORTED_LOCALE(ka, Georgian)
76+
SUPPORTED_LOCALE(de, German)
77+
SUPPORTED_LOCALE(ki, Kikuyu)
78+
SUPPORTED_LOCALE(el, Greek)
79+
SUPPORTED_LOCALE(kl, Kalaallisut)
80+
SUPPORTED_LOCALE(gn, Guarani)
81+
SUPPORTED_LOCALE(gu, Gujarati)
82+
SUPPORTED_LOCALE(ht, Haitian Creole)
83+
SUPPORTED_LOCALE(ha, Hausa)
84+
SUPPORTED_LOCALE(he, Hebrew)
85+
SUPPORTED_LOCALE(hz, Herero)
86+
SUPPORTED_LOCALE(hi, Hindi)
87+
SUPPORTED_LOCALE(ho, Hiri Motu)
88+
SUPPORTED_LOCALE(hu, Hungarian)
89+
SUPPORTED_LOCALE(is, Icelandic)
90+
SUPPORTED_LOCALE(io, Ido)
91+
SUPPORTED_LOCALE(ig, Igbo)
92+
SUPPORTED_LOCALE(id, Indonesian)
93+
SUPPORTED_LOCALE(ia, Interlingua)
94+
SUPPORTED_LOCALE(ie, Interlingue)
95+
SUPPORTED_LOCALE(iu, Inuktitut)
96+
SUPPORTED_LOCALE(ik, Inupiaq)
97+
SUPPORTED_LOCALE(ga, Irish)
98+
SUPPORTED_LOCALE(it, Italian)
99+
SUPPORTED_LOCALE(ja, Japanese)
100+
SUPPORTED_LOCALE(jv, Javanese)
101+
SUPPORTED_LOCALE(kn, Kannada)
102+
SUPPORTED_LOCALE(kr, Kanuri)
103+
SUPPORTED_LOCALE(ks, Kashmiri)
104+
SUPPORTED_LOCALE(kk, Kazakh)
105+
SUPPORTED_LOCALE(rw, Kinyarwanda)
106+
SUPPORTED_LOCALE(kv, Komi)
107+
SUPPORTED_LOCALE(kg, Kongo)
108+
SUPPORTED_LOCALE(ko, Korean)
109+
SUPPORTED_LOCALE(kj, Kwanyama)
110+
SUPPORTED_LOCALE(ku, Kurdish)
111+
SUPPORTED_LOCALE(ky, Kyrgyz)
112+
SUPPORTED_LOCALE(lo, Lao)
113+
SUPPORTED_LOCALE(la, Latin)
114+
SUPPORTED_LOCALE(lv, Latvian)
115+
SUPPORTED_LOCALE(lb, Letzeburgesch)
116+
SUPPORTED_LOCALE(li, Limburgish)
117+
SUPPORTED_LOCALE(ln, Lingala)
118+
SUPPORTED_LOCALE(lt, Lithuanian)
119+
SUPPORTED_LOCALE(lu, Luba-Katanga)
120+
SUPPORTED_LOCALE(mk, Macedonian)
121+
SUPPORTED_LOCALE(mg, Malagasy)
122+
SUPPORTED_LOCALE(ms, Malay)
123+
SUPPORTED_LOCALE(ml, Malayalam)
124+
SUPPORTED_LOCALE(mt, Maltese)
125+
SUPPORTED_LOCALE(gv, Manx)
126+
SUPPORTED_LOCALE(mi, Maori)
127+
SUPPORTED_LOCALE(mr, Marathi)
128+
SUPPORTED_LOCALE(mh, Marshallese)
129+
SUPPORTED_LOCALE(ro, Romanian)
130+
SUPPORTED_LOCALE(mn, Mongolian)
131+
SUPPORTED_LOCALE(na, Nauru)
132+
SUPPORTED_LOCALE(nv, Navajo)
133+
SUPPORTED_LOCALE(nd, Northern Ndebele)
134+
SUPPORTED_LOCALE(ng, Ndonga)
135+
SUPPORTED_LOCALE(ne, Nepali)
136+
SUPPORTED_LOCALE(se, Northern Sami)
137+
SUPPORTED_LOCALE(no, Norwegian)
138+
SUPPORTED_LOCALE(nb, Norwegian Bokmål)
139+
SUPPORTED_LOCALE(nn, Norwegian Nynorsk)
140+
SUPPORTED_LOCALE(ii, Nuosu Sichuan Yi)
141+
SUPPORTED_LOCALE(oc, Occitan)
142+
SUPPORTED_LOCALE(oj, Ojibwa)
143+
SUPPORTED_LOCALE(or, Oriya)
144+
SUPPORTED_LOCALE(om, Oromo)
145+
SUPPORTED_LOCALE(os, Ossetian)
146+
SUPPORTED_LOCALE(pi, Pali)
147+
SUPPORTED_LOCALE(pa, Panjabi)
148+
SUPPORTED_LOCALE(ps, Pashto)
149+
SUPPORTED_LOCALE(fa, Persian)
150+
SUPPORTED_LOCALE(pl, Polish)
151+
SUPPORTED_LOCALE(pt, Portuguese)
152+
SUPPORTED_LOCALE(qu, Quechua)
153+
SUPPORTED_LOCALE(rm, Romansh)
154+
SUPPORTED_LOCALE(rn, Rundi)
155+
SUPPORTED_LOCALE(ru, Russian)
156+
SUPPORTED_LOCALE(sm, Samoan)
157+
SUPPORTED_LOCALE(sg, Sango)
158+
SUPPORTED_LOCALE(sa, Sanskrit)
159+
SUPPORTED_LOCALE(sc, Sardinian)
160+
SUPPORTED_LOCALE(sr, Serbian)
161+
SUPPORTED_LOCALE(sn, Shona)
162+
SUPPORTED_LOCALE(sd, Sindhi)
163+
SUPPORTED_LOCALE(si, Sinhala Sinhalese)
164+
SUPPORTED_LOCALE(sk, Slovak)
165+
SUPPORTED_LOCALE(sl, Slovenian)
166+
SUPPORTED_LOCALE(so, Somali)
167+
SUPPORTED_LOCALE(st, Sotho Southern)
168+
SUPPORTED_LOCALE(nr, South Ndebele)
169+
SUPPORTED_LOCALE(es, Spanish Castilian)
170+
SUPPORTED_LOCALE(su, Sundanese)
171+
SUPPORTED_LOCALE(sw, Swahili)
172+
SUPPORTED_LOCALE(ss, Swati)
173+
SUPPORTED_LOCALE(sv, Swedish)
174+
SUPPORTED_LOCALE(tl, Tagalog)
175+
SUPPORTED_LOCALE(ty, Tahitian)
176+
SUPPORTED_LOCALE(tg, Tajik)
177+
SUPPORTED_LOCALE(ta, Tamil)
178+
SUPPORTED_LOCALE(tt, Tatar)
179+
SUPPORTED_LOCALE(te, Telugu)
180+
SUPPORTED_LOCALE(th, Thai)
181+
SUPPORTED_LOCALE(bo, Tibetan)
182+
SUPPORTED_LOCALE(ti, Tigrinya)
183+
SUPPORTED_LOCALE(to, Tonga)
184+
SUPPORTED_LOCALE(ts, Tsonga)
185+
SUPPORTED_LOCALE(tn, Tswana)
186+
SUPPORTED_LOCALE(tr, Turkish)
187+
SUPPORTED_LOCALE(tk, Turkmen)
188+
SUPPORTED_LOCALE(tw, Twi)
189+
SUPPORTED_LOCALE(ug, Uighur)
190+
SUPPORTED_LOCALE(uk, Ukrainian)
191+
SUPPORTED_LOCALE(ur, Urdu)
192+
SUPPORTED_LOCALE(uz, Uzbek)
193+
SUPPORTED_LOCALE(ve, Venda)
194+
SUPPORTED_LOCALE(vi, Vietnamese)
195+
SUPPORTED_LOCALE(vo, Volap_k)
196+
SUPPORTED_LOCALE(wa, Walloon)
197+
SUPPORTED_LOCALE(cy, Welsh)
198+
SUPPORTED_LOCALE(fy, Western Frisian)
199+
SUPPORTED_LOCALE(wo, Wolof)
200+
SUPPORTED_LOCALE(xh, Xhosa)
201+
SUPPORTED_LOCALE(yi, Yiddish)
202+
SUPPORTED_LOCALE(yo, Yoruba)
203+
SUPPORTED_LOCALE(za, Zhuang Chuang)
204+
SUPPORTED_LOCALE(zu, Zulu)
205+
206+
#undef SUPPORTED_LOCALE
207+

include/swift/Basic/DiagnosticOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ class DiagnosticOptions {
6767

6868
std::string DiagnosticDocumentationPath = "";
6969

70+
std::string LocalizationCode = "";
71+
72+
// Diagnostic messages directory path.
73+
std::string LocalizationPath = "";
74+
7075
/// Return a hash code of any components from these options that should
7176
/// contribute to a Swift Bridging PCH hash.
7277
llvm::hash_code getPCHHashComponents() const {

include/swift/Option/Options.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,14 @@ def diagnostic_style : Separate<["-"], "diagnostic-style">,
389389
def diagnostic_style_EQ : Joined<["-"], "diagnostic-style=">,
390390
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
391391
MetaVarName<"<style>">, Alias<diagnostic_style>;
392+
def locale : Separate<["-"], "locale">,
393+
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
394+
HelpText<"Choose a language for diagnostic messages">,
395+
MetaVarName<"<locale-code>">;
396+
def localization_path : Separate<["-"], "localization-path">,
397+
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath]>,
398+
HelpText<"Path to localized diagnostic messages directory">,
399+
MetaVarName<"<path>">;
392400

393401
def module_cache_path : Separate<["-"], "module-cache-path">,
394402
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath]>,

lib/Driver/ToolChains.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
242242
inputArgs.AddLastArg(arguments,
243243
options::OPT_emit_fine_grained_dependency_sourcefile_dot_files);
244244
inputArgs.AddLastArg(arguments, options::OPT_package_description_version);
245+
inputArgs.AddLastArg(arguments, options::OPT_locale);
246+
inputArgs.AddLastArg(arguments, options::OPT_localization_path);
245247
inputArgs.AddLastArg(arguments, options::OPT_serialize_diagnostics_path);
246248
inputArgs.AddLastArg(arguments, options::OPT_debug_diagnostic_names);
247249
inputArgs.AddLastArg(arguments, options::OPT_print_educational_notes);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ using namespace llvm::opt;
3434
/// The path for Swift libraries in the OS on Darwin.
3535
#define DARWIN_OS_LIBRARY_PATH "/usr/lib/swift"
3636

37+
static constexpr const char *const localeCodes[] = {
38+
#define SUPPORTED_LOCALE(Code, Language) #Code,
39+
#include "swift/AST/LocalizationLanguages.def"
40+
};
41+
3742
swift::CompilerInvocation::CompilerInvocation() {
3843
setTargetTriple(llvm::sys::getDefaultTargetTriple());
3944
}
@@ -57,6 +62,14 @@ void CompilerInvocation::setMainExecutablePath(StringRef Path) {
5762
llvm::sys::path::append(DiagnosticDocsPath, "share", "doc", "swift",
5863
"diagnostics");
5964
DiagnosticOpts.DiagnosticDocumentationPath = std::string(DiagnosticDocsPath.str());
65+
66+
// Compute the path of the YAML diagnostic messages directory files
67+
// in the toolchain.
68+
llvm::SmallString<128> DiagnosticMessagesDir(Path);
69+
llvm::sys::path::remove_filename(DiagnosticMessagesDir); // Remove /swift
70+
llvm::sys::path::remove_filename(DiagnosticMessagesDir); // Remove /bin
71+
llvm::sys::path::append(DiagnosticMessagesDir, "share", "swift");
72+
DiagnosticOpts.LocalizationPath = std::string(DiagnosticMessagesDir.str());
6073
}
6174

6275
void CompilerInvocation::setDefaultPrebuiltCacheIfNecessary() {
@@ -928,6 +941,43 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
928941
if (Arg *A = Args.getLastArg(OPT_diagnostic_documentation_path)) {
929942
Opts.DiagnosticDocumentationPath = A->getValue();
930943
}
944+
if (Arg *A = Args.getLastArg(OPT_locale)) {
945+
std::string localeCode = A->getValue();
946+
947+
// Check if the locale code is available.
948+
if (llvm::none_of(localeCodes, [&](const char *locale) {
949+
return localeCode == locale;
950+
})) {
951+
std::string availableLocaleCodes = "";
952+
llvm::interleave(
953+
std::begin(localeCodes), std::end(localeCodes),
954+
[&](std::string locale) { availableLocaleCodes += locale; },
955+
[&] { availableLocaleCodes += ", "; });
956+
957+
Diags.diagnose(SourceLoc(), diag::warning_invalid_locale_code,
958+
availableLocaleCodes);
959+
} else {
960+
Opts.LocalizationCode = localeCode;
961+
}
962+
}
963+
if (Arg *A = Args.getLastArg(OPT_localization_path)) {
964+
if (!llvm::sys::fs::exists(A->getValue())) {
965+
Diags.diagnose(SourceLoc(), diag::warning_locale_path_not_found,
966+
A->getValue());
967+
} else if (!Opts.LocalizationCode.empty()) {
968+
// Check if the localization path exists but it doesn't have a file
969+
// for the specified locale code.
970+
llvm::SmallString<128> localizationPath(A->getValue());
971+
llvm::sys::path::append(localizationPath, Opts.LocalizationCode);
972+
llvm::sys::path::replace_extension(localizationPath, ".yaml");
973+
if (!llvm::sys::fs::exists(localizationPath)) {
974+
Diags.diagnose(SourceLoc(), diag::warning_cannot_find_locale_file,
975+
Opts.LocalizationCode, localizationPath);
976+
}
977+
978+
Opts.LocalizationPath = A->getValue();
979+
}
980+
}
931981
assert(!(Opts.WarningsAsErrors && Opts.SuppressWarnings) &&
932982
"conflicting arguments; should have been caught by driver");
933983

lib/Frontend/Frontend.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,11 @@ void CompilerInstance::setUpDiagnosticOptions() {
410410
}
411411
Diagnostics.setDiagnosticDocumentationPath(
412412
Invocation.getDiagnosticOptions().DiagnosticDocumentationPath);
413+
if (!Invocation.getDiagnosticOptions().LocalizationCode.empty()) {
414+
Diagnostics.setLocalization(
415+
Invocation.getDiagnosticOptions().LocalizationCode,
416+
Invocation.getDiagnosticOptions().LocalizationPath);
417+
}
413418
}
414419

415420
// The ordering of ModuleLoaders is important!
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#===--- en.yaml - Localized diagnostic messages for English ---*- YAML -*-===#
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See https://swift.org/LICENSE.txt for license information
9+
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
#===----------------------------------------------------------------------===#
12+
#
13+
# This file defines the diagnostic messages for the English language.
14+
# Each diagnostic is described in the following format:
15+
# - id: "<diagnostic-id>"
16+
# msg: "<diagnostic-message>"
17+
#
18+
#===----------------------------------------------------------------------===#
19+
20+
- id: "lex_unterminated_string"
21+
msg: "unterminated string literal"
22+
23+
- id: "var_init_self_referential"
24+
msg: "variable used within its own initial value"
25+
26+
- id: "cannot_find_in_scope"
27+
msg: "cannot %select{find|find operator}1 %0 in scope"
28+
29+
- id: "warning_invalid_locale_code"
30+
msg: "unsupported locale code; supported locale codes are '%0'"

0 commit comments

Comments
 (0)