Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 17 | #include "compile/PseudolocaleGenerator.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 21 | #include "ResourceTable.h" |
| 22 | #include "ResourceValues.h" |
| 23 | #include "ValueVisitor.h" |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 24 | #include "androidfw/Util.h" |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 25 | #include "compile/Pseudolocalizer.h" |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 26 | #include "util/Util.h" |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 27 | |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 28 | using ::android::ConfigDescription; |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 29 | using ::android::StringPiece; |
| 30 | using ::android::StringPiece16; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 31 | |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 34 | // The struct that represents both Span objects and UntranslatableSections. |
| 35 | struct UnifiedSpan { |
| 36 | // Only present for Span objects. If not present, this was an UntranslatableSection. |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 37 | std::optional<std::string> tag; |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 38 | |
| 39 | // The UTF-16 index into the string where this span starts. |
| 40 | uint32_t first_char; |
| 41 | |
| 42 | // The UTF-16 index into the string where this span ends, inclusive. |
| 43 | uint32_t last_char; |
| 44 | }; |
| 45 | |
| 46 | inline static bool operator<(const UnifiedSpan& left, const UnifiedSpan& right) { |
| 47 | if (left.first_char < right.first_char) { |
| 48 | return true; |
| 49 | } else if (left.first_char > right.first_char) { |
| 50 | return false; |
| 51 | } else if (left.last_char < right.last_char) { |
| 52 | return true; |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 57 | inline static UnifiedSpan SpanToUnifiedSpan(const android::StringPool::Span& span) { |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 58 | return UnifiedSpan{*span.name, span.first_char, span.last_char}; |
| 59 | } |
| 60 | |
| 61 | inline static UnifiedSpan UntranslatableSectionToUnifiedSpan(const UntranslatableSection& section) { |
| 62 | return UnifiedSpan{ |
| 63 | {}, static_cast<uint32_t>(section.start), static_cast<uint32_t>(section.end) - 1}; |
| 64 | } |
| 65 | |
| 66 | // Merges the Span and UntranslatableSections of this StyledString into a single vector of |
| 67 | // UnifiedSpans. This will first check that the Spans are sorted in ascending order. |
| 68 | static std::vector<UnifiedSpan> MergeSpans(const StyledString& string) { |
| 69 | // Ensure the Spans are sorted and converted. |
| 70 | std::vector<UnifiedSpan> sorted_spans; |
| 71 | sorted_spans.reserve(string.value->spans.size()); |
| 72 | std::transform(string.value->spans.begin(), string.value->spans.end(), |
| 73 | std::back_inserter(sorted_spans), SpanToUnifiedSpan); |
| 74 | |
| 75 | // Stable sort to ensure tag sequences like "<b><i>" are preserved. |
| 76 | std::stable_sort(sorted_spans.begin(), sorted_spans.end()); |
| 77 | |
| 78 | // Ensure the UntranslatableSections are sorted and converted. |
| 79 | std::vector<UnifiedSpan> sorted_untranslatable_sections; |
| 80 | sorted_untranslatable_sections.reserve(string.untranslatable_sections.size()); |
| 81 | std::transform(string.untranslatable_sections.begin(), string.untranslatable_sections.end(), |
| 82 | std::back_inserter(sorted_untranslatable_sections), |
| 83 | UntranslatableSectionToUnifiedSpan); |
| 84 | std::sort(sorted_untranslatable_sections.begin(), sorted_untranslatable_sections.end()); |
| 85 | |
| 86 | std::vector<UnifiedSpan> merged_spans; |
| 87 | merged_spans.reserve(sorted_spans.size() + sorted_untranslatable_sections.size()); |
| 88 | auto span_iter = sorted_spans.begin(); |
| 89 | auto untranslatable_iter = sorted_untranslatable_sections.begin(); |
| 90 | while (span_iter != sorted_spans.end() && |
| 91 | untranslatable_iter != sorted_untranslatable_sections.end()) { |
| 92 | if (*span_iter < *untranslatable_iter) { |
| 93 | merged_spans.push_back(std::move(*span_iter)); |
| 94 | ++span_iter; |
| 95 | } else { |
| 96 | merged_spans.push_back(std::move(*untranslatable_iter)); |
| 97 | ++untranslatable_iter; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | while (span_iter != sorted_spans.end()) { |
| 102 | merged_spans.push_back(std::move(*span_iter)); |
| 103 | ++span_iter; |
| 104 | } |
| 105 | |
| 106 | while (untranslatable_iter != sorted_untranslatable_sections.end()) { |
| 107 | merged_spans.push_back(std::move(*untranslatable_iter)); |
| 108 | ++untranslatable_iter; |
| 109 | } |
| 110 | return merged_spans; |
| 111 | } |
| 112 | |
| 113 | std::unique_ptr<StyledString> PseudolocalizeStyledString(StyledString* string, |
| 114 | Pseudolocalizer::Method method, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 115 | android::StringPool* pool) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 116 | Pseudolocalizer localizer(method); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 117 | |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 118 | // Collect the spans and untranslatable sections into one set of spans, sorted by first_char. |
| 119 | // This will effectively subdivide the string into multiple sections that can be individually |
| 120 | // pseudolocalized, while keeping the span indices synchronized. |
| 121 | std::vector<UnifiedSpan> merged_spans = MergeSpans(*string); |
| 122 | |
| 123 | // All Span indices are UTF-16 based, according to the resources.arsc format expected by the |
| 124 | // runtime. So we will do all our processing in UTF-16, then convert back. |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 125 | const std::u16string text16 = android::util::Utf8ToUtf16(string->value->value); |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 126 | |
| 127 | // Convenient wrapper around the text that allows us to work with StringPieces. |
| 128 | const StringPiece16 text(text16); |
| 129 | |
| 130 | // The new string. |
| 131 | std::string new_string = localizer.Start(); |
| 132 | |
| 133 | // The stack that keeps track of what nested Span we're in. |
| 134 | std::vector<size_t> span_stack; |
| 135 | |
| 136 | // The current position in the original text. |
| 137 | uint32_t cursor = 0u; |
| 138 | |
| 139 | // The current position in the new text. |
| 140 | uint32_t new_cursor = utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(new_string.data()), |
| 141 | new_string.size(), false); |
| 142 | |
| 143 | // We assume no nesting of untranslatable sections, since XLIFF doesn't allow it. |
| 144 | bool translatable = true; |
| 145 | size_t span_idx = 0u; |
| 146 | while (span_idx < merged_spans.size() || !span_stack.empty()) { |
| 147 | UnifiedSpan* span = span_idx >= merged_spans.size() ? nullptr : &merged_spans[span_idx]; |
| 148 | UnifiedSpan* parent_span = span_stack.empty() ? nullptr : &merged_spans[span_stack.back()]; |
| 149 | |
| 150 | if (span != nullptr) { |
| 151 | if (parent_span == nullptr || parent_span->last_char > span->first_char) { |
| 152 | // There is no parent, or this span is the child of the parent. |
| 153 | // Pseudolocalize all the text until this span. |
| 154 | const StringPiece16 substr = text.substr(cursor, span->first_char - cursor); |
| 155 | cursor += substr.size(); |
| 156 | |
| 157 | // Pseudolocalize the substring. |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 158 | std::string new_substr = android::util::Utf16ToUtf8(substr); |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 159 | if (translatable) { |
| 160 | new_substr = localizer.Text(new_substr); |
| 161 | } |
| 162 | new_cursor += utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(new_substr.data()), |
| 163 | new_substr.size(), false); |
| 164 | new_string += new_substr; |
| 165 | |
| 166 | // Rewrite the first_char. |
| 167 | span->first_char = new_cursor; |
| 168 | if (!span->tag) { |
| 169 | // An untranslatable section has begun! |
| 170 | translatable = false; |
| 171 | } |
| 172 | span_stack.push_back(span_idx); |
| 173 | ++span_idx; |
| 174 | continue; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (parent_span != nullptr) { |
| 179 | // There is a parent, and either this span is not a child of it, or there are no more spans. |
| 180 | // Pop this off the stack. |
| 181 | const StringPiece16 substr = text.substr(cursor, parent_span->last_char - cursor + 1); |
| 182 | cursor += substr.size(); |
| 183 | |
| 184 | // Pseudolocalize the substring. |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 185 | std::string new_substr = android::util::Utf16ToUtf8(substr); |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 186 | if (translatable) { |
| 187 | new_substr = localizer.Text(new_substr); |
| 188 | } |
| 189 | new_cursor += utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(new_substr.data()), |
| 190 | new_substr.size(), false); |
| 191 | new_string += new_substr; |
| 192 | |
| 193 | parent_span->last_char = new_cursor - 1; |
| 194 | if (parent_span->tag) { |
| 195 | // An end to an untranslatable section. |
| 196 | translatable = true; |
| 197 | } |
| 198 | span_stack.pop_back(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Finish the pseudolocalization at the end of the string. |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 203 | new_string += |
| 204 | localizer.Text(android::util::Utf16ToUtf8(text.substr(cursor, text.size() - cursor))); |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 205 | new_string += localizer.End(); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 206 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 207 | android::StyleString localized; |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 208 | localized.str = std::move(new_string); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 209 | |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 210 | // Convert the UnifiedSpans into regular Spans, skipping the UntranslatableSections. |
| 211 | for (UnifiedSpan& span : merged_spans) { |
| 212 | if (span.tag) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 213 | localized.spans.push_back( |
| 214 | android::Span{std::move(span.tag.value()), span.first_char, span.last_char}); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 217 | return util::make_unique<StyledString>(pool->MakeRef(localized)); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | namespace { |
| 221 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 222 | class Visitor : public ValueVisitor { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 223 | public: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 224 | // Either value or item will be populated upon visiting the value. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 225 | std::unique_ptr<Value> value; |
| 226 | std::unique_ptr<Item> item; |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 227 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 228 | Visitor(android::StringPool* pool, Pseudolocalizer::Method method) |
| 229 | : pool_(pool), method_(method), localizer_(method) { |
| 230 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 231 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 232 | void Visit(Plural* plural) override { |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 233 | CloningValueTransformer cloner(pool_); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 234 | std::unique_ptr<Plural> localized = util::make_unique<Plural>(); |
| 235 | for (size_t i = 0; i < plural->values.size(); i++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 236 | Visitor sub_visitor(pool_, method_); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 237 | if (plural->values[i]) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 238 | plural->values[i]->Accept(&sub_visitor); |
Donald Chai | 4c3da0f | 2019-05-31 23:52:21 -0700 | [diff] [blame] | 239 | if (sub_visitor.item) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 240 | localized->values[i] = std::move(sub_visitor.item); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 241 | } else { |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 242 | localized->values[i] = plural->values[i]->Transform(cloner); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 243 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 244 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 245 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 246 | localized->SetSource(plural->GetSource()); |
| 247 | localized->SetWeak(true); |
| 248 | value = std::move(localized); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 249 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 250 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 251 | void Visit(String* string) override { |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 252 | const StringPiece original_string = *string->value; |
| 253 | std::string result = localizer_.Start(); |
| 254 | |
| 255 | // Pseudolocalize only the translatable sections. |
| 256 | size_t start = 0u; |
| 257 | for (const UntranslatableSection& section : string->untranslatable_sections) { |
| 258 | // Pseudolocalize the content before the untranslatable section. |
| 259 | const size_t len = section.start - start; |
| 260 | if (len > 0u) { |
| 261 | result += localizer_.Text(original_string.substr(start, len)); |
| 262 | } |
| 263 | |
| 264 | // Copy the untranslatable content. |
| 265 | result += original_string.substr(section.start, section.end - section.start); |
| 266 | start = section.end; |
| 267 | } |
| 268 | |
| 269 | // Pseudolocalize the content after the last untranslatable section. |
| 270 | if (start != original_string.size()) { |
| 271 | const size_t len = original_string.size() - start; |
| 272 | result += localizer_.Text(original_string.substr(start, len)); |
| 273 | } |
| 274 | result += localizer_.End(); |
| 275 | |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 276 | std::unique_ptr<String> localized = util::make_unique<String>(pool_->MakeRef(result)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 277 | localized->SetSource(string->GetSource()); |
| 278 | localized->SetWeak(true); |
| 279 | item = std::move(localized); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 280 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 281 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 282 | void Visit(StyledString* string) override { |
| 283 | item = PseudolocalizeStyledString(string, method_, pool_); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 284 | item->SetSource(string->GetSource()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 285 | item->SetWeak(true); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 286 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 287 | |
| 288 | private: |
| 289 | DISALLOW_COPY_AND_ASSIGN(Visitor); |
| 290 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 291 | android::StringPool* pool_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 292 | Pseudolocalizer::Method method_; |
| 293 | Pseudolocalizer localizer_; |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 294 | }; |
| 295 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 296 | ConfigDescription ModifyConfigForPseudoLocale(const ConfigDescription& base, |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 297 | Pseudolocalizer::Method m) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 298 | ConfigDescription modified = base; |
| 299 | switch (m) { |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 300 | case Pseudolocalizer::Method::kAccent: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 301 | modified.language[0] = 'e'; |
| 302 | modified.language[1] = 'n'; |
| 303 | modified.country[0] = 'X'; |
| 304 | modified.country[1] = 'A'; |
| 305 | break; |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 306 | |
| 307 | case Pseudolocalizer::Method::kBidi: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 308 | modified.language[0] = 'a'; |
| 309 | modified.language[1] = 'r'; |
| 310 | modified.country[0] = 'X'; |
| 311 | modified.country[1] = 'B'; |
| 312 | break; |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 313 | default: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 314 | break; |
| 315 | } |
| 316 | return modified; |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 319 | void PseudolocalizeIfNeeded(const Pseudolocalizer::Method method, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 320 | ResourceConfigValue* original_value, android::StringPool* pool, |
| 321 | ResourceEntry* entry) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 322 | Visitor visitor(pool, method); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 323 | original_value->value->Accept(&visitor); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 324 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 325 | std::unique_ptr<Value> localized_value; |
| 326 | if (visitor.value) { |
| 327 | localized_value = std::move(visitor.value); |
| 328 | } else if (visitor.item) { |
| 329 | localized_value = std::move(visitor.item); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 330 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 331 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 332 | if (!localized_value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 333 | return; |
| 334 | } |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 335 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 336 | ConfigDescription config_with_accent = |
| 337 | ModifyConfigForPseudoLocale(original_value->config, method); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 338 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 339 | ResourceConfigValue* new_config_value = |
| 340 | entry->FindOrCreateValue(config_with_accent, original_value->product); |
| 341 | if (!new_config_value->value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 342 | // Only use auto-generated pseudo-localization if none is defined. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 343 | new_config_value->value = std::move(localized_value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 344 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 347 | // A value is pseudolocalizable if it does not define a locale (or is the default locale) and is |
| 348 | // translatable. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 349 | static bool IsPseudolocalizable(ResourceConfigValue* config_value) { |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 350 | const int diff = config_value->config.diff(ConfigDescription::DefaultConfig()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 351 | if (diff & ConfigDescription::CONFIG_LOCALE) { |
| 352 | return false; |
| 353 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 354 | return config_value->value->IsTranslatable(); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 357 | } // namespace |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 358 | |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 359 | bool PseudolocaleGenerator::Consume(IAaptContext* context, ResourceTable* table) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 360 | for (auto& package : table->packages) { |
| 361 | for (auto& type : package->types) { |
| 362 | for (auto& entry : type->entries) { |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 363 | std::vector<ResourceConfigValue*> values = entry->FindValuesIf(IsPseudolocalizable); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 364 | for (ResourceConfigValue* value : values) { |
Adam Lesinski | 8049f3d | 2017-03-31 18:28:14 -0700 | [diff] [blame] | 365 | PseudolocalizeIfNeeded(Pseudolocalizer::Method::kAccent, value, &table->string_pool, |
| 366 | entry.get()); |
| 367 | PseudolocalizeIfNeeded(Pseudolocalizer::Method::kBidi, value, &table->string_pool, |
| 368 | entry.get()); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 369 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 370 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 371 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 372 | } |
| 373 | return true; |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 376 | } // namespace aapt |