1

Sorry if I'm not explaining the question well in the title, I'm not sure of the correct lingo. Let me give you an example instead.

So this is a normal HTML file.

<span>Well to the site!</span>

I would like to make it a bit more dynamic by perhaps having in the JS.

<script>
$scope.welcome = "Welcome to the site!";
</script>

In the HTML

<span>{{welcome}}</span>

Now I have just had a large JSON array in a separate JS file and loaded it in to the <head> and then called it.

Is this the correct way or is there a proper practice? The closest thing I can think of is Internationalization in Java.

  • I have a project that I made 100% internationalized my approach was similiar to yours. I put a meta tag as a key value storage in html. ex: <meta content="Your JSON" data-id = 'my-key-to-access-this' /> I see this approach in airbnb view-source:airbnb.com if you check the footer in airbnb they used the meta tags as a key value storage. So You could use for it. – FZE Apr 8 '16 at 20:29
  • I see but what's the benefit to loading it in a meta tag instead of just as another JS file? – jackdh Apr 8 '16 at 20:34
  • There is no problem if you use good http validation, but if you don't use correct headers, and if there is no http2 or spdy, it may make an overhead like extra socket connections etc. – FZE Apr 8 '16 at 20:41
  • As a second concern, the first one may not be a problem if you are using js, css merging and minifying. But you may not want to add all application translations to the one file. You may desire to load related page translations then it would be better to load your translations etc. dynamically. Then this approach would make more sense and if you don't want to get the json from DOM, you could also place it in a variable which is placed between <script></script> tags – FZE Apr 8 '16 at 20:57
1

If you have no additional requirements besides what you mentioned in your question, this is perfectly fine. See KISS and YAGNI.

You should only consider doing something more complicated right now if you know you're going to need something like internationalization, since deciding on a satisfactory getCorrectStringForCurrentLanguage() mechanism and porting all your existing string code to it will be much harder if you put it off until later. And even that argument only works if you already know the internationalization requirements well enough to decide on a satisfactory mechanism right now; that's not always the case.

P.S. In general, there's only a "correct way" to do something when everyone's needs are the same or very similar. It should go without saying that "displaying text to the user" is not the sort of thing where every program in the world has almost the same requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.