Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,916
    Thanks
    6
    Thanked 1,040 Times in 1,013 Posts

    Simple internationalization question

    Iím trying to print a string in different languages depending on the html elementís lang attribute and would like your input about the simplest way to do this.
    I was tinking of creating an object with properties for each language, like:
    PHP Code:
    var string = {
      
    en'english string',
      
    jp'japanese string',
      
    cn'chinese string'

    and getting the language from
    PHP Code:
    var language document.documentElement.getAttribute('lang'); 
    (note that I have full control over the contents of the lang attribute so thatís no concern for me)

    I would love to do that without many if/else statements or whatever, just by doing something like string.language so it chooses the respective property automatically but obviously it doesnít work this way.

    Any idea?

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    possibly misunderstanding completely, but does
    PHP Code:
    string[language
    come close?

  • Users who have thanked xelawho for this post:

    VIPStephan (02-27-2014)

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,916
    Thanks
    6
    Thanked 1,040 Times in 1,013 Posts
    Ha! So simple and I didn’t think of it.
    Thanks

  • #4
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,387
    Thanks
    32
    Thanked 288 Times in 282 Posts
    Quote Originally Posted by VIPStephan View Post
    Iím trying to print a string in different languages depending on the html elementís lang attribute and would like your input about the simplest way to do this.
    I was tinking of creating an object with properties for each language, like:
    PHP Code:
    var string = {
      
    en'english string',
      
    jp'japanese string',
      
    cn'chinese string'

    and getting the language from
    PHP Code:
    var language document.documentElement.getAttribute('lang'); 
    (note that I have full control over the contents of the lang attribute so thatís no concern for me)

    I would love to do that without many if/else statements or whatever, just by doing something like string.language so it chooses the respective property automatically but obviously it doesnít work this way.

    Any idea?
    This is basically the same solution as what xelawho posted with some more specifics:

    Here are HTML and XHTML solutions that use the correct ISO 639 language code for Chinese (zh) and ISO 15924 script codes for variants of Chinese. I used Object.create, which, while verbose, makes the object properties read-only and prevents creation of unnecessary properties inherited from the Object object.

    The code will need to be a bit more complicated if you're going to allow country codes or three-letter variants of two-letter codes (e.g., eng instead of en) in your lang attributes.

    HTML and XHTML:
    Code:
    <!doctype html>
    <html lang="en" xml:lang="en">
    	<head>
    		<meta charset="utf-8">
    		<title>HTML Demo</title>
    	</head>
    	<body>
    		<script>
    			// This script was validated at http://jshint.com/ using the following settings:
    			/* jshint browser: true, curly: true, eqeqeq: true, devel: false, forin: true, immed: true, latedef: true, noarg: true, noempty: true, nonew: true, plusplus: true, quotmark: double, undef: true, unused: strict, strict: true, trailing: true */
    			(function () {
    				"use strict";
    				var strings = Object.create(null);
    				var language = document.documentElement.getAttribute("lang");
    				Object.defineProperty(strings, "en", { value: "English string" });
    				Object.defineProperty(strings, "jp", { value: "Japanese string" });
    				Object.defineProperty(strings, "zh-Hans", { value: "Simplified Chinese" });
    				Object.defineProperty(strings, "zh-Hant", { value: "Traditional Chinese" });
    				alert(strings[language]);
    			})();
    		</script>
    	</body>
    </html>
    XHTML:
    Code:
    <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<title>XHTML Demo</title>
    	</head>
    	<body>
    		<script>
    			// This script was validated at http://jshint.com/ using the following settings:
    			/* jshint browser: true, curly: true, eqeqeq: true, devel: false, forin: true, immed: true, latedef: true, noarg: true, noempty: true, nonew: true, plusplus: true, quotmark: double, undef: true, unused: strict, strict: true, trailing: true */
    			(function () {
    				"use strict";
    				var namespaces = Object.create(null);
    				var strings = Object.create(null);
    				var language = null;
    				Object.defineProperty(namespaces, "XML", { value: "http://www.w3.org/XML/1998/namespace" });
    				Object.defineProperty(strings, "en", { value: "English string" });
    				Object.defineProperty(strings, "jp", { value: "Japanese string" });
    				Object.defineProperty(strings, "zh-Hans", { value: "Simplified Chinese" });
    				Object.defineProperty(strings, "zh-Hant", { value: "Traditional Chinese" });
    				language = document.documentElement.getAttributeNS(namespaces.XML, "lang");
    				alert(strings[language]);
    			})();
    		</script>
    	</body>
    </html>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #5
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,916
    Thanks
    6
    Thanked 1,040 Times in 1,013 Posts
    Right, I’m probably going to use “zh-CN” for mainland (simplified) Chinese and “zh-TW” for Taiwan (traditional Chinese — and you’re right, I also should use “ja” for Japanese). While I appreciate your efforts and your strictness (and I know that you’re very strict when I see you talking about XHTML), this is really just one or two strings in like five languages where I also have full control over the input and it’s likely not going to change until the next complete site overhaul so I’m not going through that much overhead to create the object and what not. But it’s useful knowledge nevertheless.
    Last edited by VIPStephan; 02-27-2014 at 02:52 PM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •