Home | History | Annotate | Download | only in compile
      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 
     17 #include "ResourceTable.h"
     18 #include "ResourceValues.h"
     19 #include "ValueVisitor.h"
     20 #include "compile/PseudolocaleGenerator.h"
     21 #include "compile/Pseudolocalizer.h"
     22 
     23 #include <algorithm>
     24 
     25 namespace aapt {
     26 
     27 std::unique_ptr<StyledString> pseudolocalizeStyledString(StyledString* string,
     28                                                          Pseudolocalizer::Method method,
     29                                                          StringPool* pool) {
     30     Pseudolocalizer localizer(method);
     31 
     32     const StringPiece16 originalText = *string->value->str;
     33 
     34     StyleString localized;
     35 
     36     // Copy the spans. We will update their offsets when we localize.
     37     localized.spans.reserve(string->value->spans.size());
     38     for (const StringPool::Span& span : string->value->spans) {
     39         localized.spans.push_back(Span{ *span.name, span.firstChar, span.lastChar });
     40     }
     41 
     42     // The ranges are all represented with a single value. This is the start of one range and
     43     // end of another.
     44     struct Range {
     45         size_t start;
     46 
     47         // Once the new string is localized, these are the pointers to the spans to adjust.
     48         // Since this struct represents the start of one range and end of another, we have
     49         // the two pointers respectively.
     50         uint32_t* updateStart;
     51         uint32_t* updateEnd;
     52     };
     53 
     54     auto cmp = [](const Range& r, size_t index) -> bool {
     55         return r.start < index;
     56     };
     57 
     58     // Construct the ranges. The ranges are represented like so: [0, 2, 5, 7]
     59     // The ranges are the spaces in between. In this example, with a total string length of 9,
     60     // the vector represents: (0,1], (2,4], (5,6], (7,9]
     61     //
     62     std::vector<Range> ranges;
     63     ranges.push_back(Range{ 0 });
     64     ranges.push_back(Range{ originalText.size() - 1 });
     65     for (size_t i = 0; i < string->value->spans.size(); i++) {
     66         const StringPool::Span& span = string->value->spans[i];
     67 
     68         // Insert or update the Range marker for the start of this span.
     69         auto iter = std::lower_bound(ranges.begin(), ranges.end(), span.firstChar, cmp);
     70         if (iter != ranges.end() && iter->start == span.firstChar) {
     71             iter->updateStart = &localized.spans[i].firstChar;
     72         } else {
     73             ranges.insert(iter,
     74                           Range{ span.firstChar, &localized.spans[i].firstChar, nullptr });
     75         }
     76 
     77         // Insert or update the Range marker for the end of this span.
     78         iter = std::lower_bound(ranges.begin(), ranges.end(), span.lastChar, cmp);
     79         if (iter != ranges.end() && iter->start == span.lastChar) {
     80             iter->updateEnd = &localized.spans[i].lastChar;
     81         } else {
     82             ranges.insert(iter,
     83                           Range{ span.lastChar, nullptr, &localized.spans[i].lastChar });
     84         }
     85     }
     86 
     87     localized.str += localizer.start();
     88 
     89     // Iterate over the ranges and localize each section.
     90     for (size_t i = 0; i < ranges.size(); i++) {
     91         const size_t start = ranges[i].start;
     92         size_t len = originalText.size() - start;
     93         if (i + 1 < ranges.size()) {
     94             len = ranges[i + 1].start - start;
     95         }
     96 
     97         if (ranges[i].updateStart) {
     98             *ranges[i].updateStart = localized.str.size();
     99         }
    100 
    101         if (ranges[i].updateEnd) {
    102             *ranges[i].updateEnd = localized.str.size();
    103         }
    104 
    105         localized.str += localizer.text(originalText.substr(start, len));
    106     }
    107 
    108     localized.str += localizer.end();
    109 
    110     std::unique_ptr<StyledString> localizedString = util::make_unique<StyledString>(
    111             pool->makeRef(localized));
    112     localizedString->setSource(string->getSource());
    113     return localizedString;
    114 }
    115 
    116 namespace {
    117 
    118 struct Visitor : public RawValueVisitor {
    119     StringPool* mPool;
    120     Pseudolocalizer::Method mMethod;
    121     Pseudolocalizer mLocalizer;
    122 
    123     // Either value or item will be populated upon visiting the value.
    124     std::unique_ptr<Value> mValue;
    125     std::unique_ptr<Item> mItem;
    126 
    127     Visitor(StringPool* pool, Pseudolocalizer::Method method) :
    128             mPool(pool), mMethod(method), mLocalizer(method) {
    129     }
    130 
    131     void visit(Plural* plural) override {
    132         std::unique_ptr<Plural> localized = util::make_unique<Plural>();
    133         for (size_t i = 0; i < plural->values.size(); i++) {
    134             Visitor subVisitor(mPool, mMethod);
    135             if (plural->values[i]) {
    136                 plural->values[i]->accept(&subVisitor);
    137                 if (subVisitor.mValue) {
    138                     localized->values[i] = std::move(subVisitor.mItem);
    139                 } else {
    140                     localized->values[i] = std::unique_ptr<Item>(plural->values[i]->clone(mPool));
    141                 }
    142             }
    143         }
    144         localized->setSource(plural->getSource());
    145         localized->setWeak(true);
    146         mValue = std::move(localized);
    147     }
    148 
    149     void visit(String* string) override {
    150         std::u16string result = mLocalizer.start() + mLocalizer.text(*string->value) +
    151                 mLocalizer.end();
    152         std::unique_ptr<String> localized = util::make_unique<String>(mPool->makeRef(result));
    153         localized->setSource(string->getSource());
    154         localized->setWeak(true);
    155         mItem = std::move(localized);
    156     }
    157 
    158     void visit(StyledString* string) override {
    159         mItem = pseudolocalizeStyledString(string, mMethod, mPool);
    160         mItem->setWeak(true);
    161     }
    162 };
    163 
    164 ConfigDescription modifyConfigForPseudoLocale(const ConfigDescription& base,
    165                                               Pseudolocalizer::Method m) {
    166     ConfigDescription modified = base;
    167     switch (m) {
    168     case Pseudolocalizer::Method::kAccent:
    169         modified.language[0] = 'e';
    170         modified.language[1] = 'n';
    171         modified.country[0] = 'X';
    172         modified.country[1] = 'A';
    173         break;
    174 
    175     case Pseudolocalizer::Method::kBidi:
    176         modified.language[0] = 'a';
    177         modified.language[1] = 'r';
    178         modified.country[0] = 'X';
    179         modified.country[1] = 'B';
    180         break;
    181     default:
    182         break;
    183     }
    184     return modified;
    185 }
    186 
    187 void pseudolocalizeIfNeeded(const Pseudolocalizer::Method method,
    188                             ResourceConfigValue* originalValue,
    189                             StringPool* pool,
    190                             ResourceEntry* entry) {
    191     Visitor visitor(pool, method);
    192     originalValue->value->accept(&visitor);
    193 
    194     std::unique_ptr<Value> localizedValue;
    195     if (visitor.mValue) {
    196         localizedValue = std::move(visitor.mValue);
    197     } else if (visitor.mItem) {
    198         localizedValue = std::move(visitor.mItem);
    199     }
    200 
    201     if (!localizedValue) {
    202         return;
    203     }
    204 
    205     ConfigDescription configWithAccent = modifyConfigForPseudoLocale(
    206             originalValue->config, method);
    207 
    208     ResourceConfigValue* newConfigValue = entry->findOrCreateValue(
    209             configWithAccent, originalValue->product);
    210     if (!newConfigValue->value) {
    211         // Only use auto-generated pseudo-localization if none is defined.
    212         newConfigValue->value = std::move(localizedValue);
    213     }
    214 }
    215 
    216 /**
    217  * A value is pseudolocalizable if it does not define a locale (or is the default locale)
    218  * and is translateable.
    219  */
    220 static bool isPseudolocalizable(ResourceConfigValue* configValue) {
    221     const int diff = configValue->config.diff(ConfigDescription::defaultConfig());
    222     if (diff & ConfigDescription::CONFIG_LOCALE) {
    223         return false;
    224     }
    225     return configValue->value->isTranslateable();
    226 }
    227 
    228 } // namespace
    229 
    230 bool PseudolocaleGenerator::consume(IAaptContext* context, ResourceTable* table) {
    231     for (auto& package : table->packages) {
    232         for (auto& type : package->types) {
    233             for (auto& entry : type->entries) {
    234                 std::vector<ResourceConfigValue*> values = entry->findValuesIf(isPseudolocalizable);
    235 
    236                 for (ResourceConfigValue* value : values) {
    237                     pseudolocalizeIfNeeded(Pseudolocalizer::Method::kAccent, value,
    238                                            &table->stringPool, entry.get());
    239                     pseudolocalizeIfNeeded(Pseudolocalizer::Method::kBidi, value,
    240                                            &table->stringPool, entry.get());
    241                 }
    242             }
    243         }
    244     }
    245     return true;
    246 }
    247 
    248 } // namespace aapt
    249