2015-05-25 342 views
1

我想在點擊從「開始」到「重新啓動」時更改按鈕的標籤。我的應用程序將以多種語言提供,我正在使用L10n.js庫。按鈕的標籤可以有緊跟在app.properties定義2點的值(「開始」和「重新啓動」)):當data-l10n-id屬性發生變化時,innerhtml的值不會改變

start = Start 
restart = Restart 
text = this is some text 
another-text = this is another text 

按鈕的定義如下(使用按鈕積木):

<body> 
    <p id="sometext" data-l10n-id="text"></p> 
    <section data-type="sidebar" role="region" class="skin-dark"> 
      <section style="margin-bottom: 30px"> 
       <button id="startbutton" class = "recommend" data-l10n-id="start">Start</button> 
      </section> 
     </div> 
    </section> 

一旦頁面被加載正確的按鈕(和段)值顯示。該數據本地化-id屬性和對應的值應該在點擊更改:

document.getElementById("startbutton").addEventListener("click", function(event) { 
    this.setAttribute("data-l10n-id", "restart"); 
    document.getElementById("sometext").setAttribute("data-l10n-id", "another-text"); 
}); 

看着DOM屬性已經改變,但不是值應該顯示:

<p id="sometext" data-l10n-id="another-text">this is some text</p> 
<section data-type="sidebar" role="region" class="skin-dark"> 
    <section style="margin-bottom: 30px"> 
     <button id="startbutton" class="recommend" data-l10n-id="restart">Start</button> 
    </section>   
</section> 

有什麼我做錯誤?歡迎任何評論!謝謝。

+0

您正在使用哪個版本的Firefox OS進行測試? – thomas

回答

0

這裏是爲l10n.js使用演示應用程序:https://github.com/tuxor1337/fxos-l10n-demo

注意,HTML文檔的<head>部分包含有關可用語言環境的信息:

<link rel="localization" href="locales/testing.{locale}.properties" /> 
<meta name="availableLanguages" content="en-US" /> 

此外,我們不僅包括l10n.js,但也是JavaScript的Promise,因爲這是l10n.js所要求的,但它不是Firefox OS 1.3的一部分:

<script src="es6-promise.js" defer></script> 
<script src="l10n.js" defer></script> 
<script src="app.js" defer></script> 

我用Firefox OS模擬器成功測試了版本1.3和2.0的代碼。請注意,周圍有l10n.js稍有不同的實現。我使用了gaia中使用的Mozilla實現:https://github.com/mozilla-b2g/gaia/blob/master/shared/js/l10n.js

+0

非常感謝托馬斯,它現在有效。 es6-promise.js丟失。 –

+0

另一種意見:):meta「availableLanguages」的確切作用是什麼?我應該列出所有可用的語言(如何?)或只有默認的語言? –

+0

你必須列出所有的語言,例如'' – thomas

相關問題