1

We want all our static resouces(HTML, CSS, JS) to be static pages(rather than being rendered at server), but encountered problems. In order to support multi languages, we have to embed the contexts of different languages in our code. As a result, in development, we have to compile the source files to many copies on every file changes. As the project becomes larger and larger, the compilation could be quite slow, and projects hard to manage.

Is there a best practice of supporting multi languages in single page applications?

jiyinyiyong
  • 4,586
  • 7
  • 45
  • 88
  • Check my latest question, i got some good answers for this http://stackoverflow.com/questions/21896847/dynamically-generate-javascript-in-php-or-do-ajax-for-multilanguage-interface – user3256623 Feb 24 '14 at 08:15

1 Answers1

0

I think use a Server-side script to response HTML/CSS/JS file is good approach.

A small example

index.php

<?php
    include_once 'lang.txt.php'; // Language dictionary file
    $lang = isset($_GET['lang']) ? $_GET['lang'] : 'en'; // Default is English
?>
<!-- Include JS/CSS -->
<script src="js/myScript.js.php?lang=<?=$lang?>"/>
<link href="css/style.css.php?lang=<?=$lang?>" rel="stylesheet"/>

<!-- Your HTML -->
<div class="welcome-div">
   <!-- echo text base on $lang variable -->
   You are viewing this page in <?=$getTextByLanguage[$lang]['welcome_message']?>
    <script>
        showMessage();
    </script>
</div>

myScript.js.php

<?php
    include_once 'lang.txt.php';
    $lang = isset($_GET['lang']) ? $_GET['lang'] : 'en';
?>
function showMessage() {
    alert('<?=$getTextByLanguage[$lang]['welcome_message']?>');
}

Pros

  • Won't affect page load speed, request size, browser cache files (keep performance)

Cons

  • Need more work on server-side or maybe you should build a small framework to do that.

This is just my personal idea, I think people have created some design pattern about this.

Tony Dinh
  • 6,668
  • 5
  • 39
  • 58
  • Things are much simpler if we can do such things on server side. But when they are decided the be in the client, we have to deal with the routing and packaging, even setting up the development environment can be complex. – jiyinyiyong Feb 24 '14 at 09:19