CodeIgniter: Parse dynamic language captions located in javascript

前端 未结 3 1806
囚心锁ツ
囚心锁ツ 2021-01-06 18:21

I have a javascript code that needs localisation, ie.

function js_proc() {
    var some_data = \'this text needs to be translated dynamically at runtime\';
}         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-06 18:53

    I'm not quite sure what you mean by "There are way too many js strings to be translated, so I can't pass every string as a variable", but if you are looking for some automatic parsing, this won't help.

    But this may help others, so here goes.

    For from ideal, I use a system like the following:

    In my view, I add the localizations as JavaScript global variables, using the codeigniter languages support. Something like:

    
    

    Then after that, usually,right after, I include the scripts that refernce the globals:

    
    

    inside the script:

    alert(noEmailError);
    

    will show the localized text just fine.

提交回复
热议问题