2017-02-07 1 views
1

내 js 파일에는 두 개의 JS 문자열이 있습니다. 현지화 전에자바 스크립트로 문자열의 국제화 동적 변수

JS 파일 :

var a = 'There are no stores in this area.'; 

Functions.php

wp_localize_script('store-locator', 'storelocatorjstext', array(
    'nostores' => __('There are no stores in this area.', 'storelocator') 
)); 

나는 스크립트를 지역화 위의 코드를 사용합니다. 그리고 나서 JS. 나는 현지화 한 후 다음

JS 파일을 쓰기 : 이것은 잘 작동

var a = storelocatorjstext.nostores; 

. 그러나 JS 스크립트에 동적 변수가 있으면 어떻게됩니까?

var dynamic = 5; 
var a = 'There are no stores in this area.'; 
var b = 'There are '+ dynamic +'stores in this area.'; 

Functions.php 동적 변수 및 방법 내 JS 파일에서 사용하는 방법과 문자열을 국제화하는 방법

wp_localize_script('store-locator', 'storelocatorjstext', array(
    'nostores' => __('There are no stores in this area.', 'storelocator'), 
    'existingstores' => __('There are 5 stores in this area.', 'storelocator') //I know this is wrong. How to add dynamic variable here like we do with PHP sprintf 
)); 
: 현지화하기 전에

JS 파일을 다음과 같이 ? 우리는 PHP에서 sprintf%s 또는 %d을 사용합니다. 현지화 후

JS 파일 :

var dynamic = 5; 
var a = storelocatorjstext.nostores; 
var b = ???; 

답변

1

왜 자바 스크립트 대체 사용하지, 같은 :

PHP :

wp_localize_script('store-locator', 'storelocatorjstext', array(
    'nostores' => __('There are no stores in this area.', 'storelocator'), 
    'existingstores' => __('There are %s stores in this area.', 'storelocator') //I know this is wrong. How to add dynamic variable here like we do with PHP sprintf 
)); 

JS :

var dynamic = 5; 
var a = storelocatorjstext.nostores; 
var b = storelocatorjstext.existingstores.replace("%s", dynamic); 
+1

공포 약간. 아이디어를 가져 주셔서 감사합니다. –

관련 문제