0

How should I go about avoiding hardcoding Strings in HTML code?

e.g:

<h1>Website name</h1>

So that they can be easily changed globally across the site from one location.

  • Are you willing to learn programming? ;) – djot Mar 27 '16 at 15:03
  • Hey @djot - I already am familiar with Android/Java, just recently have got into Web Development however :) Have already learnt JavaScript and PHP from earlier experiences. – harryjamesuk Mar 27 '16 at 15:04
  • Does NOT sound like that. – djot Mar 27 '16 at 15:07
  • 1
    Usually there would be a sort of placeholder inside the HTML that will be swapped with the value of a variable. This can be done after the HTML is displayed to the user or even using a template tool before the user sees the content. Using JS, an option would be to give the HMTL element an `id` or `class` and then targeting that element for the update. – Lix Mar 27 '16 at 15:10
  • Thanks @djot... In Android, we have a way to store Strings in a "strings.xml" file and then reference the strings in this file using key/value pairs. This is why I am asking this question as code repetition seriously bugs me out, especially when designing a site at which the name could change at any point, and I don't want to have to go through each page of the site updating the name. – harryjamesuk Mar 27 '16 at 15:11

1 Answers1

1

You could create separete html files with each lang, but I prefer to use another things as:

PHP:

$lang = array();

$lang['PAGE_TITLE'] = 'My website page title';
$lang['HEADER_TITLE'] = 'My website header title';
$lang['SITE_NAME'] = 'My Website';
$lang['SLOGAN'] = 'My slogan here';
$lang['HEADING'] = 'Heading';

More info for this custom method here: http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html

or:

function lang($phrase){
    static $lang = array(
        'NO_PHOTO' => 'No photo\'s available',
        'NEW_MEMBER' => 'This user is new'
    );
    return $lang[$phrase];
}

echo lang('no_photo');

(from: Most efficient way to do language file in PHP?)

Without reinventing the wheel: http://php.net/gettext

Or with javascript: Best practice javascript and multilanguage

If you use some framework (either PHP or javascript) they usually have their own multilanguage supoort (laravel, angular, zend...)

Hope it helps!

Community
  • 1
  • 1
JP. Aulet
  • 4,375
  • 4
  • 26
  • 39