1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
|
// Copyright 2010 The Closure Library Authors. All Rights Reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Message/plural format library with locale support.
*
* Message format grammar:
*
* messageFormatPattern := string ( "{" messageFormatElement "}" string )*
* messageFormatElement := argumentIndex [ "," elementFormat ]
* elementFormat := "plural" "," pluralStyle
* | "selectordinal" "," ordinalStyle
* | "select" "," selectStyle
* pluralStyle := pluralFormatPattern
* ordinalStyle := selectFormatPattern
* selectStyle := selectFormatPattern
* pluralFormatPattern := [ "offset" ":" offsetIndex ] pluralForms*
* selectFormatPattern := pluralForms*
* pluralForms := stringKey "{" ( "{" messageFormatElement "}"|string )* "}"
*
* This is a subset of the ICU MessageFormatSyntax:
* http://userguide.icu-project.org/formatparse/messages
* See also http://go/plurals and http://go/ordinals for internal details.
*
*
* Message example:
*
* I see {NUM_PEOPLE, plural, offset:1
* =0 {no one at all}
* =1 {{WHO}}
* one {{WHO} and one other person}
* other {{WHO} and # other people}}
* in {PLACE}.
*
* Calling format({'NUM_PEOPLE': 2, 'WHO': 'Mark', 'PLACE': 'Athens'}) would
* produce "I see Mark and one other person in Athens." as output.
*
* OR:
*
* {NUM_FLOOR, selectordinal,
* one {Take the elevator to the #st floor.}
* two {Take the elevator to the #nd floor.}
* few {Take the elevator to the #rd floor.}
* other {Take the elevator to the #th floor.}}
*
* Calling format({'NUM_FLOOR': 22}) would produce
* "Take the elevator to the 22nd floor".
*
* See messageformat_test.html for more examples.
*/
goog.provide('goog.i18n.MessageFormat');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.i18n.CompactNumberFormatSymbols');
goog.require('goog.i18n.NumberFormat');
goog.require('goog.i18n.NumberFormatSymbols');
goog.require('goog.i18n.ordinalRules');
goog.require('goog.i18n.pluralRules');
/**
* Constructor of MessageFormat.
* @param {string} pattern The pattern we parse and apply positional parameters
* to.
* @constructor
* @final
*/
goog.i18n.MessageFormat = function(pattern) {
/**
* The pattern we parse and apply positional parameters to.
* @type {?string}
* @private
*/
this.pattern_ = pattern;
/**
* All encountered literals during parse stage. Indices tell us the order of
* replacement.
* @type {?Array<string>}
* @private
*/
this.initialLiterals_ = null;
/**
* Working array with all encountered literals during parse and format stages.
* Indices tell us the order of replacement.
* @type {?Array<string>}
* @private
*/
this.literals_ = null;
/**
* Input pattern gets parsed into objects for faster formatting.
* @type {?Array<!Object>}
* @private
*/
this.parsedPattern_ = null;
/**
* Locale aware number formatter.
* @type {!goog.i18n.NumberFormat}
* @private
*/
this.numberFormatter_ = goog.i18n.MessageFormat.getNumberFormatter_();
};
/**
* Locale associated with the most recently created NumberFormat.
* @type {?Object}
* @private
*/
goog.i18n.MessageFormat.numberFormatterSymbols_ = null;
/**
* Locale associated with the most recently created NumberFormat.
* @type {?Object}
* @private
*/
goog.i18n.MessageFormat.compactNumberFormatterSymbols_ = null;
/**
* Locale aware number formatter. Reference to the most recently created
* NumberFormat for sharing between MessageFormat instances.
* @type {?goog.i18n.NumberFormat}
* @private
*/
goog.i18n.MessageFormat.numberFormatter_ = null;
/**
* Literal strings, including '', are replaced with \uFDDF_x_ for
* parsing purposes, and recovered during format phase.
* \uFDDF is a Unicode nonprinting character, not expected to be found in the
* typical message.
* @type {string}
* @private
*/
goog.i18n.MessageFormat.LITERAL_PLACEHOLDER_ = '\uFDDF_';
/**
* Marks a string and block during parsing.
* @enum {number}
* @private
*/
goog.i18n.MessageFormat.Element_ = {
STRING: 0,
BLOCK: 1
};
/**
* Block type.
* @enum {number}
* @private
*/
goog.i18n.MessageFormat.BlockType_ = {
PLURAL: 0,
ORDINAL: 1,
SELECT: 2,
SIMPLE: 3,
STRING: 4,
UNKNOWN: 5
};
/**
* Mandatory option in both select and plural form.
* @type {string}
* @private
*/
goog.i18n.MessageFormat.OTHER_ = 'other';
/**
* Regular expression for looking for string literals.
* @type {RegExp}
* @private
*/
goog.i18n.MessageFormat.REGEX_LITERAL_ = new RegExp("'([{}#].*?)'", 'g');
/**
* Regular expression for looking for '' in the message.
* @type {RegExp}
* @private
*/
goog.i18n.MessageFormat.REGEX_DOUBLE_APOSTROPHE_ = new RegExp("''", 'g');
/** @typedef {{ type: goog.i18n.MessageFormat.Element_, value: ? }} */
goog.i18n.MessageFormat.TypeVal_;
/**
* Gets the a NumberFormat instance for the current locale.
* If the locale is the same as the previous invocation, returns the same
* NumberFormat instance. Otherwise, creates a new one.
* @return {!goog.i18n.NumberFormat}
* @private
*/
goog.i18n.MessageFormat.getNumberFormatter_ = function() {
var currentSymbols = goog.i18n.NumberFormatSymbols;
var currentCompactSymbols = goog.i18n.CompactNumberFormatSymbols;
if (goog.i18n.MessageFormat.numberFormatterSymbols_ !== currentSymbols ||
goog.i18n.MessageFormat.compactNumberFormatterSymbols_ !==
currentCompactSymbols) {
goog.i18n.MessageFormat.numberFormatterSymbols_ = currentSymbols;
goog.i18n.MessageFormat.compactNumberFormatterSymbols_ =
currentCompactSymbols;
goog.i18n.MessageFormat.numberFormatter_ =
new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
}
return /** @type {!goog.i18n.NumberFormat} */ (
goog.i18n.MessageFormat.numberFormatter_);
};
/**
* Formats a message, treating '#' with special meaning representing
* the number (plural_variable - offset).
* @param {!Object} namedParameters Parameters that either
* influence the formatting or are used as actual data.
* I.e. in call to fmt.format({'NUM_PEOPLE': 5, 'NAME': 'Angela'}),
* object {'NUM_PEOPLE': 5, 'NAME': 'Angela'} holds positional parameters.
* 1st parameter could mean 5 people, which could influence plural format,
* and 2nd parameter is just a data to be printed out in proper position.
* @return {string} Formatted message.
*/
goog.i18n.MessageFormat.prototype.format = function(namedParameters) {
return this.format_(namedParameters, false);
};
/**
* Formats a message, treating '#' as literary character.
* @param {!Object} namedParameters Parameters that either
* influence the formatting or are used as actual data.
* I.e. in call to fmt.format({'NUM_PEOPLE': 5, 'NAME': 'Angela'}),
* object {'NUM_PEOPLE': 5, 'NAME': 'Angela'} holds positional parameters.
* 1st parameter could mean 5 people, which could influence plural format,
* and 2nd parameter is just a data to be printed out in proper position.
* @return {string} Formatted message.
*/
goog.i18n.MessageFormat.prototype.formatIgnoringPound = function(
namedParameters) {
return this.format_(namedParameters, true);
};
/**
* Formats a message.
* @param {!Object} namedParameters Parameters that either
* influence the formatting or are used as actual data.
* I.e. in call to fmt.format({'NUM_PEOPLE': 5, 'NAME': 'Angela'}),
* object {'NUM_PEOPLE': 5, 'NAME': 'Angela'} holds positional parameters.
* 1st parameter could mean 5 people, which could influence plural format,
* and 2nd parameter is just a data to be printed out in proper position.
* @param {boolean} ignorePound If true, treat '#' in plural messages as a
* literary character, else treat it as an ICU syntax character, resolving
* to the number (plural_variable - offset).
* @return {string} Formatted message.
* @private
*/
goog.i18n.MessageFormat.prototype.format_ = function(
namedParameters, ignorePound) {
this.init_();
if (!this.parsedPattern_ || this.parsedPattern_.length == 0) {
return '';
}
this.literals_ = goog.array.clone(this.initialLiterals_);
var result = [];
this.formatBlock_(this.parsedPattern_, namedParameters, ignorePound, result);
var message = result.join('');
if (!ignorePound) {
goog.asserts.assert(message.search('#') == -1, 'Not all # were replaced.');
}
while (this.literals_.length > 0) {
message = message.replace(
this.buildPlaceholder_(this.literals_), this.literals_.pop());
}
return message;
};
/**
* Parses generic block and returns a formatted string.
* @param {!Array<!goog.i18n.MessageFormat.TypeVal_>} parsedPattern
* Holds parsed tree.
* @param {!Object} namedParameters Parameters that either influence
* the formatting or are used as actual data.
* @param {boolean} ignorePound If true, treat '#' in plural messages as a
* literary character, else treat it as an ICU syntax character, resolving
* to the number (plural_variable - offset).
* @param {!Array<string>} result Each formatting stage appends its product
* to the result.
* @private
*/
goog.i18n.MessageFormat.prototype.formatBlock_ = function(
parsedPattern, namedParameters, ignorePound, result) {
for (var i = 0; i < parsedPattern.length; i++) {
switch (parsedPattern[i].type) {
case goog.i18n.MessageFormat.BlockType_.STRING:
result.push(parsedPattern[i].value);
break;
case goog.i18n.MessageFormat.BlockType_.SIMPLE:
var pattern = parsedPattern[i].value;
this.formatSimplePlaceholder_(pattern, namedParameters, result);
break;
case goog.i18n.MessageFormat.BlockType_.SELECT:
var pattern = parsedPattern[i].value;
this.formatSelectBlock_(pattern, namedParameters, ignorePound, result);
break;
case goog.i18n.MessageFormat.BlockType_.PLURAL:
var pattern = parsedPattern[i].value;
this.formatPluralOrdinalBlock_(
pattern, namedParameters, goog.i18n.pluralRules.select, ignorePound,
result);
break;
case goog.i18n.MessageFormat.BlockType_.ORDINAL:
var pattern = parsedPattern[i].value;
this.formatPluralOrdinalBlock_(
pattern, namedParameters, goog.i18n.ordinalRules.select,
ignorePound, result);
break;
default:
goog.asserts.fail('Unrecognized block type: ' + parsedPattern[i].type);
}
}
};
/**
* Formats simple placeholder.
* @param {!Object} parsedPattern JSON object containing placeholder info.
* @param {!Object} namedParameters Parameters that are used as actual data.
* @param {!Array<string>} result Each formatting stage appends its product
* to the result.
* @private
*/
goog.i18n.MessageFormat.prototype.formatSimplePlaceholder_ = function(
parsedPattern, namedParameters, result) {
var value = namedParameters[parsedPattern];
if (!goog.isDef(value)) {
result.push('Undefined parameter - ' + parsedPattern);
return;
}
// Don't push the value yet, it may contain any of # { } in it which
// will break formatter. Insert a placeholder and replace at the end.
this.literals_.push(value);
result.push(this.buildPlaceholder_(this.literals_));
};
/**
* Formats select block. Only one option is selected.
* @param {!{argumentIndex:?}} parsedPattern JSON object containing select
* block info.
* @param {!Object} namedParameters Parameters that either influence
* the formatting or are used as actual data.
* @param {boolean} ignorePound If true, treat '#' in plural messages as a
* literary character, else treat it as an ICU syntax character, resolving
* to the number (plural_variable - offset).
* @param {!Array<string>} result Each formatting stage appends its product
* to the result.
* @private
*/
goog.i18n.MessageFormat.prototype.formatSelectBlock_ = function(
parsedPattern, namedParameters, ignorePound, result) {
var argumentIndex = parsedPattern.argumentIndex;
if (!goog.isDef(namedParameters[argumentIndex])) {
result.push('Undefined parameter - ' + argumentIndex);
return;
}
var option = parsedPattern[namedParameters[argumentIndex]];
if (!goog.isDef(option)) {
option = parsedPattern[goog.i18n.MessageFormat.OTHER_];
goog.asserts.assertArray(
option, 'Invalid option or missing other option for select block.');
}
this.formatBlock_(option, namedParameters, ignorePound, result);
};
/**
* Formats plural or selectordinal block. Only one option is selected and all #
* are replaced.
* @param {!{argumentIndex, argumentOffset}} parsedPattern JSON object
* containing plural block info.
* @param {!Object} namedParameters Parameters that either influence
* the formatting or are used as actual data.
* @param {function(number, number=):string} pluralSelector A select function
* from goog.i18n.pluralRules or goog.i18n.ordinalRules which determines
* which plural/ordinal form to use based on the input number's cardinality.
* @param {boolean} ignorePound If true, treat '#' in plural messages as a
* literary character, else treat it as an ICU syntax character, resolving
* to the number (plural_variable - offset).
* @param {!Array<string>} result Each formatting stage appends its product
* to the result.
* @private
*/
goog.i18n.MessageFormat.prototype.formatPluralOrdinalBlock_ = function(
parsedPattern, namedParameters, pluralSelector, ignorePound, result) {
var argumentIndex = parsedPattern.argumentIndex;
var argumentOffset = parsedPattern.argumentOffset;
var pluralValue = +namedParameters[argumentIndex];
if (isNaN(pluralValue)) {
// TODO(user): Distinguish between undefined and invalid parameters.
result.push('Undefined or invalid parameter - ' + argumentIndex);
return;
}
var diff = pluralValue - argumentOffset;
// Check if there is an exact match.
var option = parsedPattern[namedParameters[argumentIndex]];
if (!goog.isDef(option)) {
goog.asserts.assert(diff >= 0, 'Argument index smaller than offset.');
var item;
if (this.numberFormatter_.getMinimumFractionDigits) { // number formatter?
// If we know the number of fractional digits we can make better decisions
// We can decide (for instance) between "1 dollar" and "1.00 dollars".
item = pluralSelector(
diff, this.numberFormatter_.getMinimumFractionDigits());
} else {
item = pluralSelector(diff);
}
goog.asserts.assertString(item, 'Invalid plural key.');
option = parsedPattern[item];
// If option is not provided fall back to "other".
if (!goog.isDef(option)) {
option = parsedPattern[goog.i18n.MessageFormat.OTHER_];
}
goog.asserts.assertArray(
option, 'Invalid option or missing other option for plural block.');
}
var pluralResult = [];
this.formatBlock_(option, namedParameters, ignorePound, pluralResult);
var plural = pluralResult.join('');
goog.asserts.assertString(plural, 'Empty block in plural.');
if (ignorePound) {
result.push(plural);
} else {
var localeAwareDiff = this.numberFormatter_.format(diff);
result.push(plural.replace(/#/g, localeAwareDiff));
}
};
/**
* Set up the MessageFormat.
* Parses input pattern into an array, for faster reformatting with
* different input parameters.
* Parsing is locale independent.
* @private
*/
goog.i18n.MessageFormat.prototype.init_ = function() {
if (this.pattern_) {
this.initialLiterals_ = [];
var pattern = this.insertPlaceholders_(this.pattern_);
this.parsedPattern_ = this.parseBlock_(pattern);
this.pattern_ = null;
}
};
/**
* Replaces string literals with literal placeholders.
* Literals are string of the form '}...', '{...' and '#...' where ... is
* set of characters not containing '
* Builds a dictionary so we can recover literals during format phase.
* @param {string} pattern Pattern to clean up.
* @return {string} Pattern with literals replaced with placeholders.
* @private
*/
goog.i18n.MessageFormat.prototype.insertPlaceholders_ = function(pattern) {
var literals = this.initialLiterals_;
var buildPlaceholder = goog.bind(this.buildPlaceholder_, this);
// First replace '' with single quote placeholder since they can be found
// inside other literals.
pattern = pattern.replace(
goog.i18n.MessageFormat.REGEX_DOUBLE_APOSTROPHE_, function() {
literals.push("'");
return buildPlaceholder(literals);
});
pattern = pattern.replace(
goog.i18n.MessageFormat.REGEX_LITERAL_, function(match, text) {
literals.push(text);
return buildPlaceholder(literals);
});
return pattern;
};
/**
* Breaks pattern into strings and top level {...} blocks.
* @param {string} pattern (sub)Pattern to be broken.
* @return {!Array<goog.i18n.MessageFormat.TypeVal_>}
* @private
*/
goog.i18n.MessageFormat.prototype.extractParts_ = function(pattern) {
var prevPos = 0;
var braceStack = [];
var results = [];
var braces = /[{}]/g;
braces.lastIndex = 0; // lastIndex doesn't get set to 0 so we have to.
var match;
while (match = braces.exec(pattern)) {
var pos = match.index;
if (match[0] == '}') {
var brace = braceStack.pop();
goog.asserts.assert(
goog.isDef(brace) && brace == '{', 'No matching { for }.');
if (braceStack.length == 0) {
// End of the block.
var part = {};
part.type = goog.i18n.MessageFormat.Element_.BLOCK;
part.value = pattern.substring(prevPos, pos);
results.push(part);
prevPos = pos + 1;
}
} else {
if (braceStack.length == 0) {
var substring = pattern.substring(prevPos, pos);
if (substring != '') {
results.push({
type: goog.i18n.MessageFormat.Element_.STRING,
value: substring
});
}
prevPos = pos + 1;
}
braceStack.push('{');
}
}
// Take care of the final string, and check if the braceStack is empty.
goog.asserts.assert(
braceStack.length == 0, 'There are mismatched { or } in the pattern.');
var substring = pattern.substring(prevPos);
if (substring != '') {
results.push(
{type: goog.i18n.MessageFormat.Element_.STRING, value: substring});
}
return results;
};
/**
* A regular expression to parse the plural block, extracting the argument
* index and offset (if any).
* @type {RegExp}
* @private
*/
goog.i18n.MessageFormat.PLURAL_BLOCK_RE_ =
/^\s*(\w+)\s*,\s*plural\s*,(?:\s*offset:(\d+))?/;
/**
* A regular expression to parse the ordinal block, extracting the argument
* index.
* @type {RegExp}
* @private
*/
goog.i18n.MessageFormat.ORDINAL_BLOCK_RE_ = /^\s*(\w+)\s*,\s*selectordinal\s*,/;
/**
* A regular expression to parse the select block, extracting the argument
* index.
* @type {RegExp}
* @private
*/
goog.i18n.MessageFormat.SELECT_BLOCK_RE_ = /^\s*(\w+)\s*,\s*select\s*,/;
/**
* Detects which type of a block is the pattern.
* @param {string} pattern Content of the block.
* @return {goog.i18n.MessageFormat.BlockType_} One of the block types.
* @private
*/
goog.i18n.MessageFormat.prototype.parseBlockType_ = function(pattern) {
if (goog.i18n.MessageFormat.PLURAL_BLOCK_RE_.test(pattern)) {
return goog.i18n.MessageFormat.BlockType_.PLURAL;
}
if (goog.i18n.MessageFormat.ORDINAL_BLOCK_RE_.test(pattern)) {
return goog.i18n.MessageFormat.BlockType_.ORDINAL;
}
if (goog.i18n.MessageFormat.SELECT_BLOCK_RE_.test(pattern)) {
return goog.i18n.MessageFormat.BlockType_.SELECT;
}
if (/^\s*\w+\s*/.test(pattern)) {
return goog.i18n.MessageFormat.BlockType_.SIMPLE;
}
return goog.i18n.MessageFormat.BlockType_.UNKNOWN;
};
/**
* Parses generic block.
* @param {string} pattern Content of the block to parse.
* @return {!Array<!Object>} Subblocks marked as strings, select...
* @private
*/
goog.i18n.MessageFormat.prototype.parseBlock_ = function(pattern) {
var result = [];
var parts = this.extractParts_(pattern);
for (var i = 0; i < parts.length; i++) {
var block = {};
if (goog.i18n.MessageFormat.Element_.STRING == parts[i].type) {
block.type = goog.i18n.MessageFormat.BlockType_.STRING;
block.value = parts[i].value;
} else if (goog.i18n.MessageFormat.Element_.BLOCK == parts[i].type) {
var blockType = this.parseBlockType_(parts[i].value);
switch (blockType) {
case goog.i18n.MessageFormat.BlockType_.SELECT:
block.type = goog.i18n.MessageFormat.BlockType_.SELECT;
block.value = this.parseSelectBlock_(parts[i].value);
break;
case goog.i18n.MessageFormat.BlockType_.PLURAL:
block.type = goog.i18n.MessageFormat.BlockType_.PLURAL;
block.value = this.parsePluralBlock_(parts[i].value);
break;
case goog.i18n.MessageFormat.BlockType_.ORDINAL:
block.type = goog.i18n.MessageFormat.BlockType_.ORDINAL;
block.value = this.parseOrdinalBlock_(parts[i].value);
break;
case goog.i18n.MessageFormat.BlockType_.SIMPLE:
block.type = goog.i18n.MessageFormat.BlockType_.SIMPLE;
block.value = parts[i].value;
break;
default:
goog.asserts.fail(
'Unknown block type for pattern: ' + parts[i].value);
}
} else {
goog.asserts.fail('Unknown part of the pattern.');
}
result.push(block);
}
return result;
};
/**
* Parses a select type of a block and produces JSON object for it.
* @param {string} pattern Subpattern that needs to be parsed as select pattern.
* @return {!Object} Object with select block info.
* @private
*/
goog.i18n.MessageFormat.prototype.parseSelectBlock_ = function(pattern) {
var argumentIndex = '';
var replaceRegex = goog.i18n.MessageFormat.SELECT_BLOCK_RE_;
pattern = pattern.replace(replaceRegex, function(string, name) {
argumentIndex = name;
return '';
});
var result = {};
result.argumentIndex = argumentIndex;
var parts = this.extractParts_(pattern);
// Looking for (key block)+ sequence. One of the keys has to be "other".
var pos = 0;
while (pos < parts.length) {
var key = parts[pos].value;
goog.asserts.assertString(key, 'Missing select key element.');
pos++;
goog.asserts.assert(
pos < parts.length, 'Missing or invalid select value element.');
if (goog.i18n.MessageFormat.Element_.BLOCK == parts[pos].type) {
var value = this.parseBlock_(parts[pos].value);
} else {
goog.asserts.fail('Expected block type.');
}
result[key.replace(/\s/g, '')] = value;
pos++;
}
goog.asserts.assertArray(
result[goog.i18n.MessageFormat.OTHER_],
'Missing other key in select statement.');
return result;
};
/**
* Parses a plural type of a block and produces JSON object for it.
* @param {string} pattern Subpattern that needs to be parsed as plural pattern.
* @return {!Object} Object with select block info.
* @private
*/
goog.i18n.MessageFormat.prototype.parsePluralBlock_ = function(pattern) {
var argumentIndex = '';
var argumentOffset = 0;
var replaceRegex = goog.i18n.MessageFormat.PLURAL_BLOCK_RE_;
pattern = pattern.replace(replaceRegex, function(string, name, offset) {
argumentIndex = name;
if (offset) {
argumentOffset = parseInt(offset, 10);
}
return '';
});
var result = {};
result.argumentIndex = argumentIndex;
result.argumentOffset = argumentOffset;
var parts = this.extractParts_(pattern);
// Looking for (key block)+ sequence.
var pos = 0;
while (pos < parts.length) {
var key = parts[pos].value;
goog.asserts.assertString(key, 'Missing plural key element.');
pos++;
goog.asserts.assert(
pos < parts.length, 'Missing or invalid plural value element.');
if (goog.i18n.MessageFormat.Element_.BLOCK == parts[pos].type) {
var value = this.parseBlock_(parts[pos].value);
} else {
goog.asserts.fail('Expected block type.');
}
result[key.replace(/\s*(?:=)?(\w+)\s*/, '$1')] = value;
pos++;
}
goog.asserts.assertArray(
result[goog.i18n.MessageFormat.OTHER_],
'Missing other key in plural statement.');
return result;
};
/**
* Parses an ordinal type of a block and produces JSON object for it.
* For example the input string:
* '{FOO, selectordinal, one {Message A}other {Message B}}'
* Should result in the output object:
* {
* argumentIndex: 'FOO',
* argumentOffest: 0,
* one: [ { type: 4, value: 'Message A' } ],
* other: [ { type: 4, value: 'Message B' } ]
* }
* @param {string} pattern Subpattern that needs to be parsed as plural pattern.
* @return {!Object} Object with select block info.
* @private
*/
goog.i18n.MessageFormat.prototype.parseOrdinalBlock_ = function(pattern) {
var argumentIndex = '';
var replaceRegex = goog.i18n.MessageFormat.ORDINAL_BLOCK_RE_;
pattern = pattern.replace(replaceRegex, function(string, name) {
argumentIndex = name;
return '';
});
var result = {};
result.argumentIndex = argumentIndex;
result.argumentOffset = 0;
var parts = this.extractParts_(pattern);
// Looking for (key block)+ sequence.
var pos = 0;
while (pos < parts.length) {
var key = parts[pos].value;
goog.asserts.assertString(key, 'Missing ordinal key element.');
pos++;
goog.asserts.assert(
pos < parts.length, 'Missing or invalid ordinal value element.');
if (goog.i18n.MessageFormat.Element_.BLOCK == parts[pos].type) {
var value = this.parseBlock_(parts[pos].value);
} else {
goog.asserts.fail('Expected block type.');
}
result[key.replace(/\s*(?:=)?(\w+)\s*/, '$1')] = value;
pos++;
}
goog.asserts.assertArray(
result[goog.i18n.MessageFormat.OTHER_],
'Missing other key in selectordinal statement.');
return result;
};
/**
* Builds a placeholder from the last index of the array.
* @param {!Array<string>} literals All literals encountered during parse.
* @return {string} \uFDDF_ + last index + _.
* @private
*/
goog.i18n.MessageFormat.prototype.buildPlaceholder_ = function(literals) {
goog.asserts.assert(literals.length > 0, 'Literal array is empty.');
var index = (literals.length - 1).toString(10);
return goog.i18n.MessageFormat.LITERAL_PLACEHOLDER_ + index + '_';
};
|