music
OSdata.com: programming text book 

OSdata.com

natural language translation

summary

OSdata RSS
News Feed


OSdata blog RSS.

OSdata blog RSS - add to Bloglines
OSdata blog RSS - add to Google Reader.
OSdata blog RSS - add to My AOL
OSdata blog RSS - add to Newsgator.

    This section discusses how to inject language translations into an existing web page using JavaScript.

license

    This is example code from OSdata, This Side of Sanity, and Twiddledom, released under Apache License 2.0.

    Copyright © 2012, 2013, 2014 Milo

    Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

previous page   next page
previous page
object oriented website
  next page
music player code example
Google

natural language translation

summary

    This section discusses how to inject language translations into an existing web page using JavaScript.

easy translation solution

    If you need translations on your website, there is an easy solution. Just go to Google Website Translator.

    The problem is that Google’s free tool doesn’t handle anything in JavaScript, anything injected using AJAX, or even some of the modern HTML and CSS features (although that seems to be a moving target).

    When a visitor picks a language, portions of your website might be left untranslated and some of your beautiful design might be ruined.

    If Google’s free tool works for your website, be thankful and skip the entire rest of this webpage.

    If on the other hand, Google’s free tool fails for your website, here is a simple solution.

the problem solved

    The web designer needed to build a visually interesting home page before the website infrastructure was finished.

    We set aside the home page and the designer used various free open source tools to achieve the desired visual effects.

    The programmer needed to be able to inject the translations without a huge diversion from the real website work.

    The following solution is shared for those who might have a similar situation and need.

    NOTE: Unlike most examples, I am not going to give all of the source code from the real web page. Too much of that code is from other sources and it would only confuse the reader with clutter.

    To see a working example, go to Twiddledom.com.

method

    I decided to go ahead and put all of the translations into the JavaScript rather than use methods such as AJAX. Of course, on the server side, these translations are obtained from a database and injected into the web page.

    I have set a very simple timer and once every two seconds (rough approximation) the JavaScript changes the translations used on the webpage.

put the targeted items into DIVs

    The first step was to find the targeted text in the designer’s web page.

    Once the text was found, I wrapped every item of text into a DIV with a different ID. This allows the JavaScript to modify these items on the fly.

    Note that if the designer had used canvas, the changes to the sourcce text would not be reflected on the web page. If this is your case, you will also need to force a redraw of the canvas element so that the latest translation is actually used.

<div id="slideshow1"><h3>existing text</h3>

set timer

    Next we add a function that runs a simple timer.

    There are several methods for a timer.

    One common method is to save a target time and periodically check to see if the target time has been reached (or passed). This method emphasizes an external frame rate regardless of load on the CPU and other factors that can cause delays.

    Another common method is to periodically increment a timer and check to see if a limit has been reached. This method emphasizes a steadiness of frame rate. If the CPU is heavily loaded, the animation slows down, but tends to stay at the same pace. This method also guarantees that every frame is shown, while the first method can cause frames to be skipped to catch up to the external time schedule.

    For our example we are using the easier method, a simple counter. If you need external time accuracy, modify the code to use the first method.

    Note that we have gone for such simplicity that we don’t even reset our counter. Adjust the code according to your needs.

    The call to the function (1/10th of a second time increments; note that the ID is from the artist’s work):

<body id="page" onload="intervalhandle=window.setInterval(function() {checktimedispatcher() },10);">

    And the function (we use a simple modulo to determine the passing of two seconds):

<script type="text/javascript">
<!--

var translatetime = 0;

function checktimedispatcher() {

translatetime = translatetime +1;

if ( (translatetime%200) == 0) {
  changetranslation();
}

} /* END FUNCTION checktimedispatcher */
//-->
</script>

source of translations

    Now you have the issue of the source of your translations.

    There are several for-pay translation services, including one from Google.

    Store the results in your data base. You don’t want to have to pay for the same translations over and over, plus you will get a faster response time.

free translations

    If the number of translations is small enough (such as the case the inspired this example), then you can obtain your translations for free by hand.

    One simple choice is to go to translate.google.com. Do not make the mistake of going to googletranslate.com, as that website will take over your browser in avery bad way (or at least it did when I friend typed it in a few years back).

    Type in the phrase or sentence in your language and choose each desired target language one at a time, copying and pasting the results for use on your website.

    Note that there are severe limits on the length of the text to be translated. If you run into those length problems, then create a temporary web page, insert all of your original source text, add the free Google web page translate tool mentioned above, and then translate the web page all at once. Copy and paste as before to save the target translation text. Delete the web page before it is catalogued and visitors come to it.

cleaning translations

    You may or may not need to do the following step.

    The translated text will appear in Unicode. You will not have any easy access to Google’s source code (which is transfered via AJAX and injected into your web page with JavaScript).

    I wrote a short C program to convert the Unicode back into HTML. The entire program is below.

    So far, all of the Google translations have been either in the regular ISO Latin one-byte character code or in the two-byte Unicode.

    I can obviously just add the Latin text to the existing web page.

    I need to convert the Unicode to HTML.

    I copy and paste the target translation (Unicode version) into a file with a fixed name and save. I go to Terminal and run my C program. I open up a file with a different fixed name and copy paste the resulting HTML-formatted text.

    I depend on the fact that Google seems to use a steady two-byte version. I perform a check to see if the current character is part of the standard extended ASCII character set and, if so, translate into either the single ASCII character or the standard HTML entity code for the character. Otherwise, I convert to hex and bring over the two-byte character with HTML formatting.

C code

    Here is a short C program to support translation:

/* translation to HTML program */

#include<stdio.h>

int hexconverter ( int x );
int putascii ( int rawcharacter, FILE *outfilepointer );

int main(void)
{
  FILE *outfilepointer;
  FILE *infilepointer;
  int character1;
  int character2;
  char language[16];
  char outfilename[16];
  char infilename[14];
  int firstpart1;
  int secondpart1;
  int firstpart2;
  int secondpart2;
  int spacecharacter;

  language[0] = 'f';
  language[1] = 'o';
  language[2] = 'r';
  language[3] = 'e';
  language[4] = 'i';
  language[5] = 'g';
  language[6] = 'n';
  language[7] = 'i';
  language[8] = 'n';
  language[9] = '.';
  language[10] = 't';
  language[11] = 'x';
  language[12] = 't';
  language[13] = 0;
  language[14] = 0;
  language[15] = 0;

  outfilename[0] = 'f';
  outfilename[1] = 'o';
  outfilename[2] = 'r';
  outfilename[3] = 'e';
  outfilename[4] = 'i';
  outfilename[5] = 'g';
  outfilename[6] = 'n';
  outfilename[7] = 'o';
  outfilename[8] = 'u';
  outfilename[9] = 't';
  outfilename[10] = '.';
  outfilename[11] = 'h';
  outfilename[12] = 't';
  outfilename[13] = 'm';
  outfilename[14] = 'l';
  outfilename[15] = 0;

  infilename[0] = 'f';
  infilename[1] = 'o';
  infilename[2] = 'r';
  infilename[3] = 'e';
  infilename[4] = 'i';
  infilename[5] = 'g';
  infilename[6] = 'n';
  infilename[7] = 'i';
  infilename[8] = 'n';
  infilename[9] = '.';
  infilename[10] = 't';
  infilename[11] = 'x';
  infilename[12] = 't';
  infilename[13] = 0;

  outfilepointer = fopen(outfilename, "w+");
  infilepointer = fopen(infilename, "r");

  while (!feof(infilepointer))
    {

      character1 = fgetc(infilepointer);
      if ( character1 == EOF )
        break;
      character2 = fgetc(infilepointer);
      if ( character2 == EOF )
        break;
      /* NOTE that all expected input files are an even number of characters */
      /* Therefore, second EOF check aborts and discards the odd character */

      firstpart2 = character2 / 16;
      firstpart2 = hexconverter(firstpart2);
      secondpart2 = character2 % 16;
      secondpart2 = hexconverter(secondpart2);

      firstpart1 = character1 / 16;
      firstpart1 = hexconverter(firstpart1);
      secondpart1 = character1 % 16;
      secondpart1 = hexconverter(secondpart1);

      if ( character2 == 0 )
        {
          /* fputc(character1,outfilepointer); */
          putascii(character1,outfilepointer);
        }
      else

        {
          fputc('&',outfilepointer);
          fputc('#',outfilepointer);
          fputc('x',outfilepointer);

          fputc(firstpart2,outfilepointer);
          fputc(secondpart2,outfilepointer);

          fputc(firstpart1,outfilepointer);
          fputc(secondpart1,outfilepointer);

          fputc(';',outfilepointer);
        }

    } /* END WHILE */



  fclose(outfilepointer);
  fclose(infilepointer);


  return 0;

}

int hexconverter ( int x )
  {
    switch (x)
     {
      case 10:
        x = 'a';
        break;
      case 11:
        x = 'b';
        break;
      case 12:
        x = 'c';
        break;
      case 13:
        x = 'd';
        break;
      case 14:
        x = 'e';
        break;
      case 15:
        x = 'f';
        break;
      default:
        x = x + 48;
        break;
     }

    return x;

  } /* END function hexconverter */

int putascii ( int rawcharacter, FILE *outfilepointer )
  {
    switch (rawcharacter)
     {
/* 32 is space */
      case 38: /* ampersand */
        fputc('&',outfilepointer);
        fputc('a',outfilepointer);
        fputc('m',outfilepointer);
        fputc('p',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 47: /* forward slash */
        fputc('&',outfilepointer);
        fputc('#',outfilepointer);
        fputc('4',outfilepointer);
        fputc('7',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 60: /* less than */
        fputc('&',outfilepointer);
        fputc('l',outfilepointer);
        fputc('t',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 62: /* greater than */
        fputc('&',outfilepointer);
        fputc('g',outfilepointer);
        fputc('t',outfilepointer);
        fputc(';',outfilepointer);
        break;



      case 133: /* French OE ligature */
        fputc('&',outfilepointer);
        fputc('O',outfilepointer);
        fputc('E',outfilepointer);
        fputc('l',outfilepointer);
        fputc('i',outfilepointer);
        fputc('g',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 140: /* French OE ligature */
        fputc('&',outfilepointer);
        fputc('O',outfilepointer);
        fputc('E',outfilepointer);
        fputc('l',outfilepointer);
        fputc('i',outfilepointer);
        fputc('g',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 145: /* opening single quote */
        fputc('&',outfilepointer);
        fputc('l',outfilepointer);
        fputc('s',outfilepointer);
        fputc('q',outfilepointer);
        fputc('u',outfilepointer);
        fputc('o',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 146: /* closing single quote */
        fputc('&',outfilepointer);
        fputc('r',outfilepointer);
        fputc('s',outfilepointer);
        fputc('q',outfilepointer);
        fputc('u',outfilepointer);
        fputc('o',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 147: /* opening double quote */
        fputc('&',outfilepointer);
        fputc('l',outfilepointer);
        fputc('d',outfilepointer);
        fputc('q',outfilepointer);
        fputc('u',outfilepointer);
        fputc('o',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 148: /* opening double quote */
        fputc('&',outfilepointer);
        fputc('r',outfilepointer);
        fputc('d',outfilepointer);
        fputc('q',outfilepointer);
        fputc('u',outfilepointer);
        fputc('o',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 149: /* big bullet list dot */
        fputc('&',outfilepointer);
        fputc('b',outfilepointer);
        fputc('u',outfilepointer);
        fputc('l',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 150: /* en-dash */
        fputc('&',outfilepointer);
        fputc('n',outfilepointer);
        fputc('d',outfilepointer);
        fputc('a',outfilepointer);
        fputc('s',outfilepointer);
        fputc('h',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 151: /* em-dash */
        fputc('&',outfilepointer);
        fputc('m',outfilepointer);
        fputc('d',outfilepointer);
        fputc('a',outfilepointer);
        fputc('s',outfilepointer);
        fputc('h',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 153: /* trademark symbol */
        fputc('&',outfilepointer);
        fputc('t',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('d',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 156: /* French oe ligature */
        fputc('&',outfilepointer);
        fputc('o',outfilepointer);
        fputc('e',outfilepointer);
        fputc('l',outfilepointer);
        fputc('i',outfilepointer);
        fputc('g',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 159: /* Y umlaut */
        fputc('&',outfilepointer);
        fputc('Y',outfilepointer);
        fputc('u',outfilepointer);
        fputc('m',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 160: /* non-breaking space */
        fputc('&',outfilepointer);
        fputc('n',outfilepointer);
        fputc('b',outfilepointer);
        fputc('s',outfilepointer);
        fputc('p',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 161: /* inverted exclamation point */
        fputc('&',outfilepointer);
        fputc('i',outfilepointer);
        fputc('e',outfilepointer);
        fputc('x',outfilepointer);
        fputc('c',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 162: /* cent sign */
        fputc('&',outfilepointer);
        fputc('c',outfilepointer);
        fputc('e',outfilepointer);
        fputc('n',outfilepointer);
        fputc('t',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 163: /* British pound */
        fputc('&',outfilepointer);
        fputc('p',outfilepointer);
        fputc('o',outfilepointer);
        fputc('u',outfilepointer);
        fputc('n',outfilepointer);
        fputc('d',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 164: /* generic currency symbol */
        fputc('&',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('r',outfilepointer);
        fputc('r',outfilepointer);
        fputc('e',outfilepointer);
        fputc('n',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 165: /* Japanese yen */
        fputc('&',outfilepointer);
        fputc('y',outfilepointer);
        fputc('e',outfilepointer);
        fputc('n',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 166: /* broken vertical bar */
        fputc('&',outfilepointer);
        fputc('b',outfilepointer);
        fputc('r',outfilepointer);
        fputc('v',outfilepointer);
        fputc('b',outfilepointer);
        fputc('a',outfilepointer);
        fputc('r',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 167: /* section symbol */
        fputc('&',outfilepointer);
        fputc('s',outfilepointer);
        fputc('e',outfilepointer);
        fputc('c',outfilepointer);
        fputc('t',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 168: /* spacing diaeresis */
        fputc('&',outfilepointer);
        fputc('u',outfilepointer);
        fputc('m',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 169: /* copyright symbol */
        fputc('&',outfilepointer);
        fputc('c',outfilepointer);
        fputc('o',outfilepointer);
        fputc('p',outfilepointer);
        fputc('y',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 170: /* feminine ordinal indicator */
        fputc('&',outfilepointer);
        fputc('o',outfilepointer);
        fputc('r',outfilepointer);
        fputc('d',outfilepointer);
        fputc('f',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 171: /* European/Spanish left-pointing double angle quotation mark */
        fputc('&',outfilepointer);
        fputc('l',outfilepointer);
        fputc('a',outfilepointer);
        fputc('q',outfilepointer);
        fputc('u',outfilepointer);
        fputc('o',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 172: /* not symbol */
        fputc('&',outfilepointer);
        fputc('n',outfilepointer);
        fputc('o',outfilepointer);
        fputc('t',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 173: /* soft hyphen or discretionary hyphen */
        fputc('&',outfilepointer);
        fputc('s',outfilepointer);
        fputc('h',outfilepointer);
        fputc('y',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 174: /* registered trade mark sign */
        fputc('&',outfilepointer);
        fputc('r',outfilepointer);
        fputc('e',outfilepointer);
        fputc('g',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 175: /* spacing macron */
        fputc('&',outfilepointer);
        fputc('m',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('r',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 176: /* degree symbol */
        fputc('&',outfilepointer);
        fputc('d',outfilepointer);
        fputc('e',outfilepointer);
        fputc('g',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 177: /* plus minus sign */
        fputc('&',outfilepointer);
        fputc('p',outfilepointer);
        fputc('l',outfilepointer);
        fputc('u',outfilepointer);
        fputc('s',outfilepointer);
        fputc('m',outfilepointer);
        fputc('n',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 178: /* superscript two */
        fputc('&',outfilepointer);
        fputc('s',outfilepointer);
        fputc('u',outfilepointer);
        fputc('p',outfilepointer);
        fputc('2',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 179: /* superscript three */
        fputc('&',outfilepointer);
        fputc('s',outfilepointer);
        fputc('u',outfilepointer);
        fputc('p',outfilepointer);
        fputc('3',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 180: /* spacing acute */
        fputc('&',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('t',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 181: /* micro sign */
        fputc('&',outfilepointer);
        fputc('m',outfilepointer);
        fputc('i',outfilepointer);
        fputc('c',outfilepointer);
        fputc('r',outfilepointer);
        fputc('o',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 182: /* paragraph symbol or pilcrow sign */
        fputc('&',outfilepointer);
        fputc('p',outfilepointer);
        fputc('a',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 183: /* medium middle list dot */
        fputc('&',outfilepointer);
        fputc('m',outfilepointer);
        fputc('i',outfilepointer);
        fputc('d',outfilepointer);
        fputc('d',outfilepointer);
        fputc('o',outfilepointer);
        fputc('t',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 184: /* spacing cedilla */
        fputc('&',outfilepointer);
        fputc('c',outfilepointer);
        fputc('e',outfilepointer);
        fputc('d',outfilepointer);
        fputc('i',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 185: /* superscript one */
        fputc('&',outfilepointer);
        fputc('s',outfilepointer);
        fputc('u',outfilepointer);
        fputc('p',outfilepointer);
        fputc('1',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 186: /* masculine ordinal number */
        fputc('&',outfilepointer);
        fputc('o',outfilepointer);
        fputc('r',outfilepointer);
        fputc('d',outfilepointer);
        fputc('m',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 187: /* European/Spanish right-pointing double angle quotation mark */
        fputc('&',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('q',outfilepointer);
        fputc('u',outfilepointer);
        fputc('o',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 188: /* vulgar fraction one quarter */
        fputc('&',outfilepointer);
        fputc('f',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('1',outfilepointer);
        fputc('4',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 189: /* vulgar fraction one half */
        fputc('&',outfilepointer);
        fputc('f',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('1',outfilepointer);
        fputc('2',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 190: /* vulgar fraction three quarters */
        fputc('&',outfilepointer);
        fputc('f',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('3',outfilepointer);
        fputc('4',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 191: /* inverted question mark */
        fputc('&',outfilepointer);
        fputc('i',outfilepointer);
        fputc('q',outfilepointer);
        fputc('u',outfilepointer);
        fputc('e',outfilepointer);
        fputc('s',outfilepointer);
        fputc('t',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 192: /* A grave */
        fputc('&',outfilepointer);
        fputc('A',outfilepointer);
        fputc('g',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('v',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 193: /* A acute */
        fputc('&',outfilepointer);
        fputc('A',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('t',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 194: /* A circumflex */
        fputc('&',outfilepointer);
        fputc('A',outfilepointer);
        fputc('c',outfilepointer);
        fputc('i',outfilepointer);
        fputc('r',outfilepointer);
        fputc('c',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 195: /* A tilde */
        fputc('&',outfilepointer);
        fputc('A',outfilepointer);
        fputc('t',outfilepointer);
        fputc('i',outfilepointer);
        fputc('l',outfilepointer);
        fputc('d',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 196: /* A umlaut */
        fputc('&',outfilepointer);
        fputc('A',outfilepointer);
        fputc('u',outfilepointer);
        fputc('m',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 197: /* A ring or ansgsrtom */
        fputc('&',outfilepointer);
        fputc('A',outfilepointer);
        fputc('r',outfilepointer);
        fputc('i',outfilepointer);
        fputc('n',outfilepointer);
        fputc('g',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 198: /* AE ligature */
        fputc('&',outfilepointer);
        fputc('A',outfilepointer);
        fputc('E',outfilepointer);
        fputc('l',outfilepointer);
        fputc('i',outfilepointer);
        fputc('g',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 199: /* French C cedille */
        fputc('&',outfilepointer);
        fputc('C',outfilepointer);
        fputc('c',outfilepointer);
        fputc('e',outfilepointer);
        fputc('d',outfilepointer);
        fputc('i',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 200: /* E grave */
        fputc('&',outfilepointer);
        fputc('E',outfilepointer);
        fputc('g',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('v',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 201: /* E acute */
        fputc('&',outfilepointer);
        fputc('E',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('t',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 202: /* E circumflex */
        fputc('&',outfilepointer);
        fputc('E',outfilepointer);
        fputc('c',outfilepointer);
        fputc('i',outfilepointer);
        fputc('r',outfilepointer);
        fputc('c',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 203: /* E umlaut */
        fputc('&',outfilepointer);
        fputc('E',outfilepointer);
        fputc('u',outfilepointer);
        fputc('m',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 204: /* I grave */
        fputc('&',outfilepointer);
        fputc('I',outfilepointer);
        fputc('g',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('v',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 205: /* I acute */
        fputc('&',outfilepointer);
        fputc('I',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('t',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 206: /* I circumflex */
        fputc('&',outfilepointer);
        fputc('I',outfilepointer);
        fputc('c',outfilepointer);
        fputc('i',outfilepointer);
        fputc('r',outfilepointer);
        fputc('c',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 207: /* I umlaut */
        fputc('&',outfilepointer);
        fputc('I',outfilepointer);
        fputc('u',outfilepointer);
        fputc('m',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 208: /* Old English Eth */
        fputc('&',outfilepointer);
        fputc('E',outfilepointer);
        fputc('T',outfilepointer);
        fputc('H',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 209: /* N tilde */
        fputc('&',outfilepointer);
        fputc('N',outfilepointer);
        fputc('t',outfilepointer);
        fputc('i',outfilepointer);
        fputc('l',outfilepointer);
        fputc('d',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 210: /* O grave */
        fputc('&',outfilepointer);
        fputc('O',outfilepointer);
        fputc('g',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('v',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 211: /* O acute */
        fputc('&',outfilepointer);
        fputc('O',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('t',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 212: /* O circumflex */
        fputc('&',outfilepointer);
        fputc('O',outfilepointer);
        fputc('c',outfilepointer);
        fputc('i',outfilepointer);
        fputc('r',outfilepointer);
        fputc('c',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 213: /* O tilde */
        fputc('&',outfilepointer);
        fputc('O',outfilepointer);
        fputc('t',outfilepointer);
        fputc('i',outfilepointer);
        fputc('l',outfilepointer);
        fputc('d',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 214: /* O umlaut */
        fputc('&',outfilepointer);
        fputc('O',outfilepointer);
        fputc('u',outfilepointer);
        fputc('m',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 215: /* multiplication sign */
        fputc('&',outfilepointer);
        fputc('t',outfilepointer);
        fputc('i',outfilepointer);
        fputc('m',outfilepointer);
        fputc('e',outfilepointer);
        fputc('s',outfilepointer);
        fputc('h',outfilepointer);
        break;
      case 216: /* O slash */
        fputc('&',outfilepointer);
        fputc('O',outfilepointer);
        fputc('s',outfilepointer);
        fputc('l',outfilepointer);
        fputc('a',outfilepointer);
        fputc('s',outfilepointer);
        fputc('h',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 217: /* U grave */
        fputc('&',outfilepointer);
        fputc('U',outfilepointer);
        fputc('g',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('v',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 218: /* U acute */
        fputc('&',outfilepointer);
        fputc('U',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('t',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 219: /* U circumflex */
        fputc('&',outfilepointer);
        fputc('U',outfilepointer);
        fputc('c',outfilepointer);
        fputc('i',outfilepointer);
        fputc('r',outfilepointer);
        fputc('c',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 220: /* U umlaut */
        fputc('&',outfilepointer);
        fputc('U',outfilepointer);
        fputc('u',outfilepointer);
        fputc('m',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 221: /* Y acute */
        fputc('&',outfilepointer);
        fputc('Y',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('t',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 222: /* Old English Thorn */
        fputc('&',outfilepointer);
        fputc('T',outfilepointer);
        fputc('H',outfilepointer);
        fputc('O',outfilepointer);
        fputc('R',outfilepointer);
        fputc('N',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 223: /* German double S */
        fputc('&',outfilepointer);
        fputc('s',outfilepointer);
        fputc('z',outfilepointer);
        fputc('l',outfilepointer);
        fputc('i',outfilepointer);
        fputc('g',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 224: /* a grave */
        fputc('&',outfilepointer);
        fputc('a',outfilepointer);
        fputc('g',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('v',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 225: /* a acute */
        fputc('&',outfilepointer);
        fputc('a',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('t',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 226: /* a circumflex */
        fputc('&',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('i',outfilepointer);
        fputc('r',outfilepointer);
        fputc('c',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 227: /* a tilde */
        fputc('&',outfilepointer);
        fputc('a',outfilepointer);
        fputc('t',outfilepointer);
        fputc('i',outfilepointer);
        fputc('l',outfilepointer);
        fputc('d',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 228: /* a umlaut */
        fputc('&',outfilepointer);
        fputc('a',outfilepointer);
        fputc('u',outfilepointer);
        fputc('m',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 229: /* a ring or ansgsrtom */
        fputc('&',outfilepointer);
        fputc('a',outfilepointer);
        fputc('r',outfilepointer);
        fputc('i',outfilepointer);
        fputc('n',outfilepointer);
        fputc('g',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 230: /* ae ligature */
        fputc('&',outfilepointer);
        fputc('a',outfilepointer);
        fputc('e',outfilepointer);
        fputc('l',outfilepointer);
        fputc('i',outfilepointer);
        fputc('g',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 231: /* French c cedille */
        fputc('&',outfilepointer);
        fputc('c',outfilepointer);
        fputc('c',outfilepointer);
        fputc('e',outfilepointer);
        fputc('d',outfilepointer);
        fputc('i',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 232: /* e grave */
        fputc('&',outfilepointer);
        fputc('e',outfilepointer);
        fputc('g',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('v',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 233: /* e acute */
        fputc('&',outfilepointer);
        fputc('e',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('t',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 234: /* e circumflex */
        fputc('&',outfilepointer);
        fputc('e',outfilepointer);
        fputc('c',outfilepointer);
        fputc('i',outfilepointer);
        fputc('r',outfilepointer);
        fputc('c',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 235: /* e umlaut */
        fputc('&',outfilepointer);
        fputc('e',outfilepointer);
        fputc('u',outfilepointer);
        fputc('m',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 236: /* i grave */
        fputc('&',outfilepointer);
        fputc('i',outfilepointer);
        fputc('g',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('v',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 237: /* i acute */
        fputc('&',outfilepointer);
        fputc('i',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('t',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 238: /* i circumflex */
        fputc('&',outfilepointer);
        fputc('i',outfilepointer);
        fputc('c',outfilepointer);
        fputc('i',outfilepointer);
        fputc('r',outfilepointer);
        fputc('c',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 239: /* i umlaut */
        fputc('&',outfilepointer);
        fputc('i',outfilepointer);
        fputc('u',outfilepointer);
        fputc('m',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 240: /* Old English eth */
        fputc('&',outfilepointer);
        fputc('e',outfilepointer);
        fputc('t',outfilepointer);
        fputc('h',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 241: /* n tilde */
        fputc('&',outfilepointer);
        fputc('n',outfilepointer);
        fputc('t',outfilepointer);
        fputc('i',outfilepointer);
        fputc('l',outfilepointer);
        fputc('d',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 242: /* o grave */
        fputc('&',outfilepointer);
        fputc('o',outfilepointer);
        fputc('g',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('v',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 243: /* o acute */
        fputc('&',outfilepointer);
        fputc('o',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('t',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 244: /* o circumflex */
        fputc('&',outfilepointer);
        fputc('o',outfilepointer);
        fputc('c',outfilepointer);
        fputc('i',outfilepointer);
        fputc('r',outfilepointer);
        fputc('c',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 245: /* o tilde */
        fputc('&',outfilepointer);
        fputc('o',outfilepointer);
        fputc('t',outfilepointer);
        fputc('i',outfilepointer);
        fputc('l',outfilepointer);
        fputc('d',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 246: /* o umlaut */
        fputc('&',outfilepointer);
        fputc('o',outfilepointer);
        fputc('u',outfilepointer);
        fputc('m',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 247: /* division symbol */
        fputc('&',outfilepointer);
        fputc('d',outfilepointer);
        fputc('i',outfilepointer);
        fputc('v',outfilepointer);
        fputc('i',outfilepointer);
        fputc('d',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 248: /* o slash */
        fputc('&',outfilepointer);
        fputc('o',outfilepointer);
        fputc('s',outfilepointer);
        fputc('l',outfilepointer);
        fputc('a',outfilepointer);
        fputc('s',outfilepointer);
        fputc('h',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 249: /* u grave */
        fputc('&',outfilepointer);
        fputc('u',outfilepointer);
        fputc('g',outfilepointer);
        fputc('r',outfilepointer);
        fputc('a',outfilepointer);
        fputc('v',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 250: /* u acute */
        fputc('&',outfilepointer);
        fputc('u',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('t',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 251: /* u circumflex */
        fputc('&',outfilepointer);
        fputc('u',outfilepointer);
        fputc('c',outfilepointer);
        fputc('i',outfilepointer);
        fputc('r',outfilepointer);
        fputc('c',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 252: /* u umlaut */
        fputc('&',outfilepointer);
        fputc('u',outfilepointer);
        fputc('u',outfilepointer);
        fputc('m',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 253: /* y acute */
        fputc('&',outfilepointer);
        fputc('y',outfilepointer);
        fputc('a',outfilepointer);
        fputc('c',outfilepointer);
        fputc('u',outfilepointer);
        fputc('t',outfilepointer);
        fputc('e',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 254: /* Old English thorn */
        fputc('&',outfilepointer);
        fputc('t',outfilepointer);
        fputc('h',outfilepointer);
        fputc('o',outfilepointer);
        fputc('r',outfilepointer);
        fputc('n',outfilepointer);
        fputc(';',outfilepointer);
        break;
      case 255: /* y umlaut */
        fputc('&',outfilepointer);
        fputc('y',outfilepointer);
        fputc('u',outfilepointer);
        fputc('m',outfilepointer);
        fputc('l',outfilepointer);
        fputc(';',outfilepointer);
        break;
      default:
        fputc(rawcharacter,outfilepointer);
        break;
     } /* END SWITCH */

    return 0;

  } /* END function putascii */

languages

    For our example, we have two languages: the main language (which starts matching the agent string form the browser) and the alternate langauge.

    We present each phrase in the main language and then present an alternate language beneath. In our case, we change the alternate language every two seconds.

var currentlanguage = 15; /* English */
var alternatelanguage;

language changing function

    And here is a portion of the language changing function. The entire function is not shown because you will be using different language choices.

function changetranslation() {

/* used to change all of the translations at the designated time */

    We use a random number generator to choose the next alternate language.

/* switch languages */ l
anguagechanger = Math.floor(Math.random() * 50));

    We use a switch statement to set the new alternate language.

switch(languagechanger) {

case 0:
  alternatelanguage = 3; /* Arabic */
  languagename = "Arabic";
  break;
case 1:
  alternatelanguage = 7; /* Bengali */
  languagename = "Bengali";
  break;
case 2:
  alternatelanguage = 10; /* Chinese */
  languagename = "Chinese";
  break;

skipping many similar settings

} /* END language switch */

    In our case, we show the current alternate language on screen. This next snippet of the function updates that item on the web page.

document.getElementById("languagename").innerHTML= languagename;

    We set the various elements. We format new HTML with the proper translation injected and then update the webpage. I am showing only the first two items, as the method is the same for every element.

header1 = "<h3>";
header2 = "<h3 style=\"color:#fff\">";

firstline1 = translateline(currentlanguage,1);
secondline1 = translateline(alternatelanguage,1);
firstline2 = translateline(currentlanguage,2);
secondline2 = translateline(alternatelanguage,2);

I will explain the translateline function below

footer = "</h3>";

document.getElementById("slideshow1").innerHTML= header1 + firstline1 + "<br>" + secondline1 + footer;

/* block dispaly of alternate translation if it is the exact same as the source langauge */

if ( secondline2 == "Media") {
  document.getElementById("slideshow2").innerHTML= header2 + firstline2 + footer;
}
else {
  document.getElementById("slideshow2").innerHTML= header2 + firstline2 + "<br>" + secondline2 + footer;
}

    And end the function.

return false;
} /* END FUNCTION changetranslation */

translation function

    And, finally, the function to perform the translations.

    We provide two pieces of information:

    (1) The alternate language. This is a numeric code (each langauge is assigned a unique number).

    (2) A line number. Also a numeric code. Each element that will be translated is assigned its own numeric code.

    Again, I simplify the function by only showing the first two examples (both using pure Unicode) and one example of language that uses the Latin (extended ASCII) character set. All of these translations were created using translate.google.com and the little C program listed above.

function translateline(language, linenumber) {

/* used to translate one line at a time */

switch(language) {

  case 3: /* Arabic */
    switch (linenumber) {
      case 1: returnline = "&#x0639;&#x0628;&#x062b; &#x0628;&#x0647;&#x0627;"; break; /*
عبث بها */
      case 2: returnline = "&#x0648;&#x0633;&#x0627;&#x0626;&#x0644; &#x0627;&#x0644;&#x0625;&#x0639;&#x0644;&#x0627;&#x0645;"; break; /*
وسائل الإعلام */
    } /* END switch linenumber */
    break;

  case 10: /* Chinese */
    switch (linenumber) {
      case 1: returnline = "&#x65cb;&#x5f04;&#x4ed6;&#x5011;"; break; /*
旋弄他們 */
      case 2: returnline = "&#x5a92;&#x9ad4;"; break; /*
媒體 */
    } /* END switch linenumber */
    break;

  case 14: /* Dutch */
    switch (linenumber) {
      case 1: returnline = "Het Einde van RSS &Eacute;&eacute;n"; break;
      case 2: returnline = "Media"; break;
    } /* END switch linenumber */
    break;

} /* END language switch */

return returnline;

} /* END FUNCTION translateline */

    And it is as simple as that!

return to source code lessons

Tweets by @outrageouscoder
Tweets by @osdata

return to Outrageous Coder
return to Source Code Examples

previous page   next page
previous page
object oriented website
  next page
music player code example

contact
comments, suggestions, corrections, criticisms

because of ridiculous spam attacks,
contact through Twitter (@OutrageousCoder) will be more reliable than the contact form

please contact us

your name:
email address:
phone number:
message:

return to Outrageous Coder
return to source code lessons
return to table of contents
free downloadable college text book

license

    This is example code from OSdata, This Side of Sanity, and Twiddledom, released under Apache License 2.0.

    Copyright © 2012, 2013, 2014 Milo

    Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

previous page   next page
previous page
object oriented website
  next page
music player code example

Google


Made with Macintosh

    This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

Viewable With Any Browser


    †UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

    Names and logos of various OSs are trademarks of their respective owners.

    Copyright © 2014 Milo

    Created: September 15, 2014

    Last Updated: September 15, 2014


return to table of contents
free downloadable college text book

previous page   next page
previous page
object oriented website
  next page
music player code example