Scott Tsai | 8a2b908 | 2009-03-21 08:08:36 +0800 | [diff] [blame] | 1 | #include <cstdio> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | #include "XLIFFFile.h" |
| 3 | #include "ValuesFile.h" |
| 4 | #include "localize.h" |
Jack Palevich | bdb087c | 2009-06-24 19:01:27 -0700 | [diff] [blame] | 5 | #include <stdio.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 6 | |
| 7 | int pseudolocalize_xliff(XLIFFFile* xliff, bool expand); |
| 8 | |
| 9 | static int |
| 10 | test_filename(const string& file, const string& locale, const string& expected) |
| 11 | { |
| 12 | string result = translated_file_name(file, locale); |
| 13 | if (result != expected) { |
| 14 | fprintf(stderr, "translated_file_name test failed\n"); |
| 15 | fprintf(stderr, " locale='%s'\n", locale.c_str()); |
| 16 | fprintf(stderr, " expected='%s'\n", expected.c_str()); |
| 17 | fprintf(stderr, " result='%s'\n", result.c_str()); |
| 18 | return 1; |
| 19 | } else { |
| 20 | if (false) { |
| 21 | fprintf(stderr, "translated_file_name test passed\n"); |
| 22 | fprintf(stderr, " locale='%s'\n", locale.c_str()); |
| 23 | fprintf(stderr, " expected='%s'\n", expected.c_str()); |
| 24 | fprintf(stderr, " result='%s'\n", result.c_str()); |
| 25 | } |
| 26 | return 0; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | static int |
| 31 | translated_file_name_test() |
| 32 | { |
| 33 | bool all = true; |
| 34 | int err = 0; |
| 35 | |
| 36 | if (all) err |= test_filename("//device/samples/NotePad/res/values/strings.xml", "zz_ZZ", |
| 37 | "//device/samples/NotePad/res/values-zz-rZZ/strings.xml"); |
| 38 | |
| 39 | if (all) err |= test_filename("//device/samples/NotePad/res/values/strings.xml", "zz", |
| 40 | "//device/samples/NotePad/res/values-zz/strings.xml"); |
| 41 | |
| 42 | if (all) err |= test_filename("//device/samples/NotePad/res/values/strings.xml", "", |
| 43 | "//device/samples/NotePad/res/values/strings.xml"); |
| 44 | |
| 45 | return err; |
| 46 | } |
| 47 | |
| 48 | bool |
| 49 | return_false(const string&, const TransUnit& unit, void* cookie) |
| 50 | { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | static int |
| 55 | delete_trans_units() |
| 56 | { |
| 57 | XLIFFFile* xliff = XLIFFFile::Parse("testdata/strip_xliff.xliff"); |
| 58 | if (xliff == NULL) { |
| 59 | printf("couldn't read file\n"); |
| 60 | return 1; |
| 61 | } |
| 62 | if (false) { |
| 63 | printf("XLIFF was [[%s]]\n", xliff->ToString().c_str()); |
| 64 | } |
| 65 | |
| 66 | xliff->Filter(return_false, NULL); |
| 67 | |
| 68 | if (false) { |
| 69 | printf("XLIFF is [[%s]]\n", xliff->ToString().c_str()); |
| 70 | |
| 71 | set<StringResource> const& strings = xliff->GetStringResources(); |
| 72 | printf("strings.size=%zd\n", strings.size()); |
| 73 | for (set<StringResource>::iterator it=strings.begin(); it!=strings.end(); it++) { |
| 74 | const StringResource& str = *it; |
| 75 | printf("STRING!!! id=%s value='%s' pos=%s file=%s version=%d(%s)\n", str.id.c_str(), |
| 76 | str.value->ContentsToString(ANDROID_NAMESPACES).c_str(), |
| 77 | str.pos.ToString().c_str(), str.file.c_str(), str.version, |
| 78 | str.versionString.c_str()); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int |
| 86 | filter_trans_units() |
| 87 | { |
| 88 | XLIFFFile* xliff = XLIFFFile::Parse("testdata/strip_xliff.xliff"); |
| 89 | if (xliff == NULL) { |
| 90 | printf("couldn't read file\n"); |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | if (false) { |
| 95 | printf("XLIFF was [[%s]]\n", xliff->ToString().c_str()); |
| 96 | } |
| 97 | |
| 98 | Settings setting; |
| 99 | xliff->Filter(keep_this_trans_unit, &setting); |
| 100 | |
| 101 | if (false) { |
| 102 | printf("XLIFF is [[%s]]\n", xliff->ToString().c_str()); |
| 103 | |
| 104 | set<StringResource> const& strings = xliff->GetStringResources(); |
| 105 | printf("strings.size=%zd\n", strings.size()); |
| 106 | for (set<StringResource>::iterator it=strings.begin(); it!=strings.end(); it++) { |
| 107 | const StringResource& str = *it; |
| 108 | printf("STRING!!! id=%s value='%s' pos=%s file=%s version=%d(%s)\n", str.id.c_str(), |
| 109 | str.value->ContentsToString(ANDROID_NAMESPACES).c_str(), |
| 110 | str.pos.ToString().c_str(), str.file.c_str(), str.version, |
| 111 | str.versionString.c_str()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int |
| 119 | settings_test() |
| 120 | { |
| 121 | int err; |
| 122 | map<string,Settings> settings; |
| 123 | map<string,Settings>::iterator it; |
| 124 | |
| 125 | err = read_settings("testdata/config.xml", &settings, "//asdf"); |
| 126 | if (err != 0) { |
| 127 | return err; |
| 128 | } |
| 129 | |
| 130 | if (false) { |
| 131 | for (it=settings.begin(); it!=settings.end(); it++) { |
| 132 | const Settings& setting = it->second; |
| 133 | printf("CONFIG:\n"); |
| 134 | printf(" id='%s'\n", setting.id.c_str()); |
| 135 | printf(" oldVersion='%s'\n", setting.oldVersion.c_str()); |
| 136 | printf(" currentVersion='%s'\n", setting.currentVersion.c_str()); |
| 137 | int i=0; |
| 138 | for (vector<string>::const_iterator app=setting.apps.begin(); |
| 139 | app!=setting.apps.end(); app++) { |
| 140 | printf(" apps[%02d]='%s'\n", i, app->c_str()); |
| 141 | i++; |
| 142 | } |
| 143 | i=0; |
| 144 | for (vector<Reject>::const_iterator reject=setting.reject.begin(); |
| 145 | reject!=setting.reject.end(); reject++) { |
| 146 | i++; |
| 147 | printf(" reject[%02d]=('%s','%s','%s')\n", i, reject->file.c_str(), |
| 148 | reject->name.c_str(), reject->comment.c_str()); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | for (it=settings.begin(); it!=settings.end(); it++) { |
| 154 | const Settings& setting = it->second; |
| 155 | if (it->first != setting.id) { |
| 156 | fprintf(stderr, "it->first='%s' setting.id='%s'\n", it->first.c_str(), |
| 157 | setting.id.c_str()); |
| 158 | err |= 1; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
| 163 | return err; |
| 164 | } |
| 165 | |
| 166 | static int |
| 167 | test_one_pseudo(bool big, const char* expected) |
| 168 | { |
| 169 | XLIFFFile* xliff = XLIFFFile::Parse("testdata/pseudo.xliff"); |
| 170 | if (xliff == NULL) { |
| 171 | printf("couldn't read file\n"); |
| 172 | return 1; |
| 173 | } |
| 174 | if (false) { |
| 175 | printf("XLIFF was [[%s]]\n", xliff->ToString().c_str()); |
| 176 | } |
| 177 | |
| 178 | pseudolocalize_xliff(xliff, big); |
| 179 | string newString = xliff->ToString(); |
| 180 | delete xliff; |
| 181 | |
| 182 | if (false) { |
| 183 | printf("XLIFF is [[%s]]\n", newString.c_str()); |
| 184 | } |
| 185 | |
| 186 | if (false && newString != expected) { |
| 187 | fprintf(stderr, "xliff didn't translate as expected\n"); |
| 188 | fprintf(stderr, "newString=[[%s]]\n", newString.c_str()); |
| 189 | fprintf(stderr, "expected=[[%s]]\n", expected); |
| 190 | return 1; |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int |
| 197 | pseudolocalize_test() |
| 198 | { |
| 199 | int err = 0; |
| 200 | |
| 201 | err |= test_one_pseudo(false, ""); |
| 202 | //err |= test_one_pseudo(true, ""); |
| 203 | |
| 204 | return err; |
| 205 | } |
| 206 | |
| 207 | int |
| 208 | localize_test() |
| 209 | { |
| 210 | bool all = true; |
| 211 | int err = 0; |
| 212 | |
| 213 | if (all) err |= translated_file_name_test(); |
| 214 | if (all) err |= delete_trans_units(); |
| 215 | if (all) err |= filter_trans_units(); |
| 216 | if (all) err |= settings_test(); |
| 217 | if (all) err |= pseudolocalize_test(); |
| 218 | |
| 219 | return err; |
| 220 | } |
| 221 | |