| 537 | | } |
| 538 | | |
| 539 | | // Localization routines |
| 540 | | |
| 541 | | dojo.hostenv.normalizeLocale = function(/*String?*/locale){ |
| 542 | | // summary: |
| 543 | | // Returns canonical form of locale, as used by Dojo. All variants |
| 544 | | // are case-insensitive and are separated by '-' as specified in RFC |
| 545 | | // 3066. If no locale is specified, the user agent's default is |
| 546 | | // returned. |
| 547 | | |
| 548 | | var result = locale ? locale.toLowerCase() : dojo.locale; |
| 549 | | if(result == "root"){ |
| 550 | | result = "ROOT"; |
| 551 | | } |
| 552 | | return result;// String |
| 553 | | }; |
| 554 | | |
| 555 | | dojo.hostenv.searchLocalePath = function(/*String*/locale, /*Boolean*/down, /*Function*/searchFunc){ |
| 556 | | // summary: |
| 557 | | // A helper method to assist in searching for locale-based resources. |
| 558 | | // Will iterate through the variants of a particular locale, either up |
| 559 | | // or down, executing a callback function. For example, "en-us" and |
| 560 | | // true will try "en-us" followed by "en" and finally "ROOT". |
| 561 | | |
| 562 | | locale = dojo.hostenv.normalizeLocale(locale); |
| 563 | | |
| 564 | | var elements = locale.split('-'); |
| 565 | | var searchlist = []; |
| 566 | | for(var i = elements.length; i > 0; i--){ |
| 567 | | searchlist.push(elements.slice(0, i).join('-')); |
| 568 | | } |
| 569 | | searchlist.push(false); |
| 570 | | if(down){searchlist.reverse();} |
| 571 | | |
| 572 | | for(var j = searchlist.length - 1; j >= 0; j--){ |
| 573 | | var loc = searchlist[j] || "ROOT"; |
| 574 | | var stop = searchFunc(loc); |
| 575 | | if(stop){ break; } |
| 576 | | } |
| 577 | | } |
| 578 | | |
| 579 | | //These two functions are placed outside of preloadLocalizations |
| 580 | | //So that the xd loading can use/override them. |
| 581 | | dojo.hostenv.localesGenerated /***BUILD:localesGenerated***/; // value will be inserted here at build time, if necessary |
| 582 | | |
| 583 | | dojo.hostenv.registerNlsPrefix = function(){ |
| 584 | | // summary: |
| 585 | | // Register module "nls" to point where Dojo can find pre-built |
| 586 | | // localization files |
| 587 | | dojo.registerModulePath("nls","nls"); |
| 588 | | } |
| 589 | | |
| 590 | | dojo.hostenv.preloadLocalizations = function(){ |
| 591 | | // summary: |
| 592 | | // Load built, flattened resource bundles, if available for all |
| 593 | | // locales used in the page. Execute only once. Note that this is a |
| 594 | | // no-op unless there is a build. |
| 595 | | |
| 596 | | if(dojo.hostenv.localesGenerated){ |
| 597 | | dojo.hostenv.registerNlsPrefix(); |
| 598 | | |
| 599 | | function preload(locale){ |
| 600 | | locale = dojo.hostenv.normalizeLocale(locale); |
| 601 | | dojo.hostenv.searchLocalePath(locale, true, function(loc){ |
| 602 | | for(var i=0; i<dojo.hostenv.localesGenerated.length;i++){ |
| 603 | | if(dojo.hostenv.localesGenerated[i] == loc){ |
| 604 | | dojo["require"]("nls.dojo_"+loc); |
| 605 | | return true; // Boolean |
| 606 | | } |
| 607 | | } |
| 608 | | return false; // Boolean |
| 609 | | }); |
| 610 | | } |
| 611 | | preload(); |
| 612 | | var extra = djConfig.extraLocale||[]; |
| 613 | | for(var i=0; i<extra.length; i++){ |
| 614 | | preload(extra[i]); |
| 615 | | } |
| 616 | | } |
| 617 | | dojo.hostenv.preloadLocalizations = function(){}; |
| 682 | | dojo.hostenv.preloadLocalizations(); |
| 683 | | var targetLocale = dojo.hostenv.normalizeLocale(locale); |
| 684 | | var bundlePackage = [moduleName, "nls", bundleName].join("."); |
| 685 | | // NOTE: |
| 686 | | // When loading these resources, the packaging does not match what is |
| 687 | | // on disk. This is an implementation detail, as this is just a |
| 688 | | // private data structure to hold the loaded resources. e.g. |
| 689 | | // tests/hello/nls/en-us/salutations.js is loaded as the object |
| 690 | | // tests.hello.nls.salutations.en_us={...} The structure on disk is |
| 691 | | // intended to be most convenient for developers and translators, but |
| 692 | | // in memory it is more logical and efficient to store in a different |
| 693 | | // order. Locales cannot use dashes, since the resulting path will |
| 694 | | // not evaluate as valid JS, so we translate them to underscores. |
| 695 | | |
| 696 | | //Find the best-match locale to load if we have available flat locales. |
| 697 | | var bestLocale = ""; |
| 698 | | if(availableFlatLocales){ |
| 699 | | var flatLocales = availableFlatLocales.split(","); |
| 700 | | for(var i = 0; i < flatLocales.length; i++){ |
| 701 | | //Locale must match from start of string. |
| 702 | | if(targetLocale.indexOf(flatLocales[i]) == 0){ |
| 703 | | if(flatLocales[i].length > bestLocale.length){ |
| 704 | | bestLocale = flatLocales[i]; |
| 705 | | } |
| 706 | | } |
| 707 | | } |
| 708 | | if(!bestLocale){ |
| 709 | | bestLocale = "ROOT"; |
| 710 | | } |
| 711 | | } |
| 712 | | |
| 713 | | //See if the desired locale is already loaded. |
| 714 | | var tempLocale = availableFlatLocales ? bestLocale : targetLocale; |
| 715 | | var bundle = dojo.hostenv.findModule(bundlePackage); |
| 716 | | var localizedBundle = null; |
| 717 | | if(bundle){ |
| 718 | | if(djConfig.localizationComplete && bundle._built){return;} |
| 719 | | var jsLoc = tempLocale.replace('-', '_', 'g'); |
| 720 | | var translationPackage = bundlePackage+"."+jsLoc; |
| 721 | | localizedBundle = dojo.hostenv.findModule(translationPackage); |
| 722 | | } |
| 723 | | |
| 724 | | if(!localizedBundle){ |
| 725 | | bundle = dojo.hostenv.startPackage(bundlePackage); |
| 726 | | var syms = dojo.hostenv.getModuleSymbols(moduleName); |
| 727 | | var modpath = syms.concat("nls").join("/"); |
| 728 | | var parent; |
| 729 | | |
| 730 | | dojo.hostenv.searchLocalePath(tempLocale, availableFlatLocales, function(loc){ |
| 731 | | var jsLoc = loc.replace('-', '_', 'g'); |
| 732 | | var translationPackage = bundlePackage + "." + jsLoc; |
| 733 | | var loaded = false; |
| 734 | | if(!dojo.hostenv.findModule(translationPackage)){ |
| 735 | | // Mark loaded whether it's found or not, so that further load attempts will not be made |
| 736 | | dojo.hostenv.startPackage(translationPackage); |
| 737 | | var module = [modpath]; |
| 738 | | if(loc != "ROOT"){module.push(loc);} |
| 739 | | module.push(bundleName); |
| 740 | | var filespec = module.join("/") + '.js'; |
| 741 | | loaded = dojo.hostenv.loadPath(filespec, null, function(hash){ |
| 742 | | // Use singleton with prototype to point to parent bundle, then mix-in result from loadPath |
| 743 | | var clazz = function(){}; |
| 744 | | clazz.prototype = parent; |
| 745 | | bundle[jsLoc] = new clazz(); |
| 746 | | for(var j in hash){ bundle[jsLoc][j] = hash[j]; } |
| 747 | | }); |
| 748 | | }else{ |
| 749 | | loaded = true; |
| 750 | | } |
| 751 | | if(loaded && bundle[jsLoc]){ |
| 752 | | parent = bundle[jsLoc]; |
| 753 | | }else{ |
| 754 | | bundle[jsLoc] = parent; |
| 755 | | } |
| 756 | | |
| 757 | | if(availableFlatLocales){ |
| 758 | | //Stop the locale path searching if we know the availableFlatLocales, since |
| 759 | | //the first call to this function will load the only bundle that is needed. |
| 760 | | return true; |
| 761 | | } |
| 762 | | }); |
| 763 | | } |
| 764 | | |
| 765 | | //Save the best locale bundle as the target locale bundle when we know the |
| 766 | | //the available bundles. |
| 767 | | if(availableFlatLocales && targetLocale != bestLocale){ |
| 768 | | bundle[targetLocale.replace('-', '_', 'g')] = bundle[bestLocale.replace('-', '_', 'g')]; |
| 769 | | } |
| 770 | | }; |
| 771 | | |
| 772 | | (function(){ |
| 773 | | // If other locales are used, dojo.requireLocalization should load them as |
| 774 | | // well, by default. |
| 775 | | // |
| 776 | | // Override dojo.requireLocalization to do load the default bundle, then |
| 777 | | // iterate through the extraLocale list and load those translations as |
| 778 | | // well, unless a particular locale was requested. |
| 779 | | |
| 780 | | var extra = djConfig.extraLocale; |
| 781 | | if(extra){ |
| 782 | | if(!extra instanceof Array){ |
| 783 | | extra = [extra]; |
| 784 | | } |
| 785 | | |
| 786 | | var req = dojo.requireLocalization; |
| 787 | | dojo.requireLocalization = function(m, b, locale, availableFlatLocales){ |
| 788 | | req(m,b,locale, availableFlatLocales); |
| 789 | | if(locale){return;} |
| 790 | | for(var i=0; i<extra.length; i++){ |
| 791 | | req(m,b,extra[i], availableFlatLocales); |
| 792 | | } |
| 793 | | }; |
| 794 | | } |
| 795 | | })(); |
| 796 | | // vim:ai:ts=4:noet:textwidth=80 |
| | 601 | dojo.require("dojo.i18n.loader"); |
| | 602 | dojo.i18n._requireLocalization.apply(dojo.hostenv, arguments); |
| | 603 | } |