1

我的网站上有以下语言选择器小部件:

jQuery(function($) {

  var setLng = $.url().param('setLng');
  var setLngCookie = $.cookie('i18next');
    var language;

  if (setLngCookie) {
        language = setLngCookie;
  }
  else {
        if (setLng) {
            language_complete = setLng.split("-");
        } else {
            language_complete = navigator.language.split("-");
        }

        language = (language_complete[0]);
  }

  //Build the language selector.
    $.getJSON("../locales/config.json", function(objData) {
      var arrHtml = [];
        var strClass;

      $.each(objData, function(strKey, objLanguage) {
            strClass = (language == objLanguage.locale) ? " class=\"selected-language\"" : ""; //Make sure the language selector has the correct language selected on initial page load.
            arrHtml.push("<a id=\"lang-" + objLanguage.locale + "\" data-i18n=\"language." + objLanguage.locale + "\"" + strClass + "></a>");
      });

      $("<div/>", {
            'class': 'language-list',
            html: arrHtml.join('')
      }).appendTo('#language-menu');

        $("li.language-menu").live("click", function(){
            $("#language-menu").toggle();
        });
    });

  function setLanguage() {
    // save to use translation function as resources are fetched
    $(".tzm-i18n").i18n();
    $(".page-i18n").i18n();
    $(".menu").i18n();
    $(".user-menu").i18n();
    $(".search-form").i18n();
    $(".footer-i18n").i18n();

        $("#language-menu").hide();
  }

  i18n.init({
    lng: language,
    debug: true
  }, setLanguage);

  // language selector
  $("#language-menu a").live("click", function() {
    var booReload = false; // TRUE = reload the page; FALSE = do not reload the page
        var $this = $(this);
    var value = $this.attr("id");
    var arrValueParts = value.split("-");
    var language = arrValueParts[1];

    if (booReload) {
      window.location.href = "/index.html?setLng=" + language;
    } else {
      i18n.init({
        lng: language,
        debug: true
      }, setLanguage);
    }

        $("#language-menu a").removeClass("selected-language");
        $this.addClass("selected-language");
  });

});

(zmgc)☺  tree -L 2 locales 
locales
├── README.md
├── config.json
├── de
│   └── translation.json
├── dev
│   └── translation.json
├── en
│   └── translation.json
├── fr
│   └── translation.json
├── jp
│   └── translation.json
├── pt
│   └── translation.json
├── rs
│   └── translation.json
├── ru
│   └── translation.json
└── us
    └── translation.json

所以每次我在 locals/xx 中添加一个目录,其中 xx 是来自 IETF 语言标签的国家代码,我必须手动更新 ../locales/config.json !

locals/config.json

{
    "de": {
        "locale": "de",
        "code": "de-DE"
    },
    "en": {
        "locale": "en",
        "code": "en-UK"
    },
    "fr": {
        "locale": "fr",
        "code": "fr-FR"
    },
    "jp": {
        "locale": "jp",
        "code": "jp-JP"
    },
    "pt": {
        "locale": "pt",
        "code": "pt-BR"
    },
    "rs": {
        "locale": "rs",
        "code": "rs-RS"
    },
    "ru": {
        "locale": "ru",
        "code": "ru-RU"
    },
    "us": {
        "locale": "us",
        "code": "en-US"
    }
}

例如 locals/en/translation.json

{
  "locale": "en"
  ,"code": "en-UK"
  ,"tzm": "Chapters - Zeitgeist Movement"
  ,"menu": {
    "projects": "Projects"
    ,"map": "Map"
    ,"calendar": "Calendar"
    ,"forums": "Forums"
    ,"more": "More »"
  }
  ,"search": {
    "advanced": "Advanced"
    ,"search": "Search"
  }
  ,"user": {
    "gravatar": "Gravatar"
    ,"profile": "Profile"
    ,"login": "Login"
    ,"log-out": "Log out"
    ,"contact": "Contact"
    ,"help": "Help"
  }
  ,"footer": {
    "zeitgeist": "Zeitgeist"
  }
}

有没有办法通过遍历 locals/ 目录并构建可用语言并从可用语言列表中排除 locals/dev 来构建它?

4

2 回答 2

1

不是来自 JavaScript。

我建议您在服务器上构建一个脚本,该脚本将为您生成配置文件。然后,您将能够在客户端上使用相同的代码。

于 2013-01-16T13:38:50.817 回答
1

除非您通过 HTTP 公开目录,否则不会;JavaScript 在客户端上执行,通常不会不受限制地访问服务器端目录布局。

在服务器端构建配置文件,或通过 API 公开文件列表。

于 2013-01-16T13:41:35.667 回答