Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 393b5f0d authored by Adam Lesinski's avatar Adam Lesinski
Browse files

AAPT2: Port AAPT pseudolocalization to AAPT2

Pseudolocalization happens at the compile phase. Pseudolocalized
values are weak, such that manually specified values will take precedence.

Change-Id: I5e064ce0d270c9f4f9022f75aecedab9d45bc980
parent 24b8ff0f
Loading
Loading
Loading
Loading
+4 −0
Original line number Original line Diff line number Diff line
@@ -27,6 +27,8 @@ main := Main.cpp
sources := \
sources := \
	compile/IdAssigner.cpp \
	compile/IdAssigner.cpp \
	compile/Png.cpp \
	compile/Png.cpp \
	compile/PseudolocaleGenerator.cpp \
	compile/Pseudolocalizer.cpp \
	compile/XmlIdCollector.cpp \
	compile/XmlIdCollector.cpp \
	flatten/Archive.cpp \
	flatten/Archive.cpp \
	flatten/TableFlattener.cpp \
	flatten/TableFlattener.cpp \
@@ -66,6 +68,8 @@ sources := \


testSources := \
testSources := \
	compile/IdAssigner_test.cpp \
	compile/IdAssigner_test.cpp \
	compile/PseudolocaleGenerator_test.cpp \
	compile/Pseudolocalizer_test.cpp \
	compile/XmlIdCollector_test.cpp \
	compile/XmlIdCollector_test.cpp \
	flatten/FileExportWriter_test.cpp \
	flatten/FileExportWriter_test.cpp \
	flatten/TableFlattener_test.cpp \
	flatten/TableFlattener_test.cpp \
+7 −2
Original line number Original line Diff line number Diff line
@@ -564,8 +564,10 @@ bool ResourceParser::parseString(xml::XmlPullParser* parser, ParsedResource* out
        return false;
        return false;
    }
    }


    if (formatted && translateable) {
    if (String* stringValue = valueCast<String>(outResource->value.get())) {
    if (String* stringValue = valueCast<String>(outResource->value.get())) {
        stringValue->setTranslateable(translateable);

        if (formatted && translateable) {
            if (!util::verifyJavaStringFormat(*stringValue->value)) {
            if (!util::verifyJavaStringFormat(*stringValue->value)) {
                mDiag->error(DiagMessage(outResource->source)
                mDiag->error(DiagMessage(outResource->source)
                             << "multiple substitutions specified in non-positional format; "
                             << "multiple substitutions specified in non-positional format; "
@@ -573,6 +575,9 @@ bool ResourceParser::parseString(xml::XmlPullParser* parser, ParsedResource* out
                return false;
                return false;
            }
            }
        }
        }

    } else if (StyledString* stringValue = valueCast<StyledString>(outResource->value.get())) {
        stringValue->setTranslateable(translateable);
    }
    }
    return true;
    return true;
}
}
+21 −16
Original line number Original line Diff line number Diff line
@@ -36,10 +36,6 @@ void BaseItem<Derived>::accept(RawValueVisitor* visitor) {
    visitor->visit(static_cast<Derived*>(this));
    visitor->visit(static_cast<Derived*>(this));
}
}


bool Value::isWeak() const {
    return false;
}

RawString::RawString(const StringPool::Ref& ref) : value(ref) {
RawString::RawString(const StringPool::Ref& ref) : value(ref) {
}
}


@@ -101,10 +97,6 @@ void Reference::print(std::ostream* out) const {
    }
    }
}
}


bool Id::isWeak() const {
    return true;
}

bool Id::flatten(android::Res_value* out) const {
bool Id::flatten(android::Res_value* out) const {
    out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
    out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
    out->data = util::hostToDevice32(0);
    out->data = util::hostToDevice32(0);
@@ -119,7 +111,15 @@ void Id::print(std::ostream* out) const {
    *out << "(id)";
    *out << "(id)";
}
}


String::String(const StringPool::Ref& ref) : value(ref) {
String::String(const StringPool::Ref& ref) : value(ref), mTranslateable(true) {
}

void String::setTranslateable(bool val) {
    mTranslateable = val;
}

bool String::isTranslateable() const {
    return mTranslateable;
}
}


bool String::flatten(android::Res_value* outValue) const {
bool String::flatten(android::Res_value* outValue) const {
@@ -144,7 +144,15 @@ void String::print(std::ostream* out) const {
    *out << "(string) \"" << *value << "\"";
    *out << "(string) \"" << *value << "\"";
}
}


StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {
StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref), mTranslateable(true) {
}

void StyledString::setTranslateable(bool val) {
    mTranslateable = val;
}

bool StyledString::isTranslateable() const {
    return mTranslateable;
}
}


bool StyledString::flatten(android::Res_value* outValue) const {
bool StyledString::flatten(android::Res_value* outValue) const {
@@ -238,13 +246,10 @@ void BinaryPrimitive::print(std::ostream* out) const {
}
}


Attribute::Attribute(bool w, uint32_t t) :
Attribute::Attribute(bool w, uint32_t t) :
        weak(w), typeMask(t),
        typeMask(t),
        minInt(std::numeric_limits<int32_t>::min()),
        minInt(std::numeric_limits<int32_t>::min()),
        maxInt(std::numeric_limits<int32_t>::max()) {
        maxInt(std::numeric_limits<int32_t>::max()) {
}
    mWeak = w;

bool Attribute::isWeak() const {
    return weak;
}
}


Attribute* Attribute::clone(StringPool* /*newPool*/) const {
Attribute* Attribute::clone(StringPool* /*newPool*/) const {
@@ -359,7 +364,7 @@ void Attribute::print(std::ostream* out) const {
            << "]";
            << "]";
    }
    }


    if (weak) {
    if (isWeak()) {
        *out << " [weak]";
        *out << " [weak]";
    }
    }
}
}
+26 −5
Original line number Original line Diff line number Diff line
@@ -43,9 +43,15 @@ struct Value {


    /**
    /**
     * Whether this value is weak and can be overridden without
     * Whether this value is weak and can be overridden without
     * warning or error. Default for base class is false.
     * warning or error. Default is false.
     */
     */
    virtual bool isWeak() const;
    bool isWeak() const {
        return mWeak;
    }

    void setWeak(bool val) {
        mWeak = val;
    }


    /**
    /**
     * Returns the source where this value was defined.
     * Returns the source where this value was defined.
@@ -95,6 +101,7 @@ struct Value {
protected:
protected:
    Source mSource;
    Source mSource;
    std::u16string mComment;
    std::u16string mComment;
    bool mWeak = false;
};
};


/**
/**
@@ -159,7 +166,7 @@ struct Reference : public BaseItem<Reference> {
 * An ID resource. Has no real value, just a place holder.
 * An ID resource. Has no real value, just a place holder.
 */
 */
struct Id : public BaseItem<Id> {
struct Id : public BaseItem<Id> {
    bool isWeak() const override;
    Id() { mWeak = true; }
    bool flatten(android::Res_value* out) const override;
    bool flatten(android::Res_value* out) const override;
    Id* clone(StringPool* newPool) const override;
    Id* clone(StringPool* newPool) const override;
    void print(std::ostream* out) const override;
    void print(std::ostream* out) const override;
@@ -185,9 +192,17 @@ struct String : public BaseItem<String> {


    String(const StringPool::Ref& ref);
    String(const StringPool::Ref& ref);


    // Whether the string is marked as translateable. This does not persist when flattened.
    // It is only used during compilation phase.
    void setTranslateable(bool val);
    bool isTranslateable() const;

    bool flatten(android::Res_value* outValue) const override;
    bool flatten(android::Res_value* outValue) const override;
    String* clone(StringPool* newPool) const override;
    String* clone(StringPool* newPool) const override;
    void print(std::ostream* out) const override;
    void print(std::ostream* out) const override;

private:
    bool mTranslateable;
};
};


struct StyledString : public BaseItem<StyledString> {
struct StyledString : public BaseItem<StyledString> {
@@ -195,9 +210,17 @@ struct StyledString : public BaseItem<StyledString> {


    StyledString(const StringPool::StyleRef& ref);
    StyledString(const StringPool::StyleRef& ref);


    // Whether the string is marked as translateable. This does not persist when flattened.
    // It is only used during compilation phase.
    void setTranslateable(bool val);
    bool isTranslateable() const;

    bool flatten(android::Res_value* outValue) const override;
    bool flatten(android::Res_value* outValue) const override;
    StyledString* clone(StringPool* newPool) const override;
    StyledString* clone(StringPool* newPool) const override;
    void print(std::ostream* out) const override;
    void print(std::ostream* out) const override;

private:
    bool mTranslateable;
};
};


struct FileReference : public BaseItem<FileReference> {
struct FileReference : public BaseItem<FileReference> {
@@ -232,7 +255,6 @@ struct Attribute : public BaseValue<Attribute> {
        uint32_t value;
        uint32_t value;
    };
    };


	bool weak;
    uint32_t typeMask;
    uint32_t typeMask;
    int32_t minInt;
    int32_t minInt;
    int32_t maxInt;
    int32_t maxInt;
@@ -240,7 +262,6 @@ struct Attribute : public BaseValue<Attribute> {


    Attribute(bool w, uint32_t t = 0u);
    Attribute(bool w, uint32_t t = 0u);


    bool isWeak() const override;
    Attribute* clone(StringPool* newPool) const override;
    Attribute* clone(StringPool* newPool) const override;
    void printMask(std::ostream* out) const;
    void printMask(std::ostream* out) const;
    void print(std::ostream* out) const override;
    void print(std::ostream* out) const override;
+14 −0
Original line number Original line Diff line number Diff line
@@ -21,6 +21,7 @@
#include "ResourceTable.h"
#include "ResourceTable.h"
#include "compile/IdAssigner.h"
#include "compile/IdAssigner.h"
#include "compile/Png.h"
#include "compile/Png.h"
#include "compile/PseudolocaleGenerator.h"
#include "compile/XmlIdCollector.h"
#include "compile/XmlIdCollector.h"
#include "flatten/Archive.h"
#include "flatten/Archive.h"
#include "flatten/FileExportWriter.h"
#include "flatten/FileExportWriter.h"
@@ -105,6 +106,7 @@ struct CompileOptions {
    std::string outputPath;
    std::string outputPath;
    Maybe<std::string> resDir;
    Maybe<std::string> resDir;
    Maybe<std::u16string> product;
    Maybe<std::u16string> product;
    bool pseudolocalize = false;
    bool verbose = false;
    bool verbose = false;
};
};


@@ -203,6 +205,16 @@ static bool compileTable(IAaptContext* context, const CompileOptions& options,
        fin.close();
        fin.close();
    }
    }


    if (options.pseudolocalize) {
        // Generate pseudo-localized strings (en-XA and ar-XB).
        // These are created as weak symbols, and are only generated from default configuration
        // strings and plurals.
        PseudolocaleGenerator pseudolocaleGenerator;
        if (!pseudolocaleGenerator.consume(context, &table)) {
            return false;
        }
    }

    // Ensure we have the compilation package at least.
    // Ensure we have the compilation package at least.
    table.createPackage(context->getCompilationPackage());
    table.createPackage(context->getCompilationPackage());


@@ -423,6 +435,8 @@ int compile(const std::vector<StringPiece>& args) {
            .requiredFlag("-o", "Output path", &options.outputPath)
            .requiredFlag("-o", "Output path", &options.outputPath)
            .optionalFlag("--product", "Product type to compile", &product)
            .optionalFlag("--product", "Product type to compile", &product)
            .optionalFlag("--dir", "Directory to scan for resources", &options.resDir)
            .optionalFlag("--dir", "Directory to scan for resources", &options.resDir)
            .optionalSwitch("--pseudo-localize", "Generate resources for pseudo-locales "
                            "(en-XA and ar-XB)", &options.pseudolocalize)
            .optionalSwitch("-v", "Enables verbose logging", &options.verbose);
            .optionalSwitch("-v", "Enables verbose logging", &options.verbose);
    if (!flags.parse("aapt2 compile", args, &std::cerr)) {
    if (!flags.parse("aapt2 compile", args, &std::cerr)) {
        return 1;
        return 1;
Loading