config file vs. file headers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #1

    config file vs. file headers

    Hi,

    I could need some advice. In my script I'm reading a language translation file (some hundred entries). which file I select depends on the user selections (i.e. $_GET['lang']). my problem is currently, how to decide which file to load. additionally, if there's ever a new file added (or changed), I don't want the user to have to dig through a lot of code to get it working.

    way 1: make a configuration file, which lists which values (the short language names like de, en, …) map to which file.
    Code:
    $map = parse_ini_file(config.lang.ini);
    /* giving:
    $map = array
    (
        "en" => "English.ini",
        "de" => "German.ini",
        …
    ); */
    $translation = parse_ini_file($map[$language]);
    Drawback: I need an additional config file/the config file must be updated

    way 2: the language files contain a value of the appropriate abbreviation so I could read that value and decide, if I use this file or not.
    Code:
    // English.ini
    ; some preceding lines 
    ; of comment
    …
    lang_short = "en"
    …
    
    // PHP
    foreach ($files_from_readdir as $ini_file)
    {
        $file = parse_ini_file($ini_file);
        if ($file['short_lang'] == $language) 
        {
            $translation = $file;
            break;
        }
    }
    Drawback: in the worst case I have to parse all files, before I get the right one.

    way 3: I could think of some code using fread() or file() and stop after the "lang_short " line.

    any ideas what the best/most elegant way is?

    thanks
  • Ciary
    Recognized Expert New Member
    • Apr 2009
    • 247

    #2
    if al your language-files are in the same directory you can use this:

    Code:
    $array = glob($path."/*");
    
    foreach($array as $k => $v){
       $array[$k] = str_replace(".ini","",$v);
    }
    after this, $array will contain all languages

    then you can simply let your user select the right file. i dont know if this is exacly what you need, but i think it might be useful.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      Originally posted by Ciary
      then you can simply let your user select the right file. i dont know if this is exacly what you need, but i think it might be useful.
      well, not exactly. the problem is not to get the file names (if I want to display them to the user, I need to translate them before that) but to select the correct file once I have the URL parameter value.

      currently I'm using
      Code:
      	/**
      	 * set the $lang property by looking for a "lang" attribute (URL).
      	 * read the appropriate language file.
      	 */
      	private function getLanguage()
      	{
      		# read language parameter from $_GET
      		if (isset($_GET['lang']) and strlen($_GET['lang']) == 2)
      		{
      			# gets me "en", "de", … or "" (mostly "")
      			$this->lang = $_GET['lang'];
      		}
      
      [B]// that's what I currently use but is this the best way?
      [/B]		# read language configuration
      		[B]$lang_conf = parse_ini_file(MYPHP_GB_DIR . 'lang/' . 'config.language.ini');[/B]
      		
      		$lang_file = MYPHP_GB_DIR . 'lang/';
      		
      		# select system language
      		if (array_key_exists($this->lang, $lang_conf))
      		{
      			[B]$lang_file .= $lang_conf[$this->lang];[/B]
      		}
      		elseif ($this->properties['language'])
      		{
      			# read default from DB
      			$lang_file .= $this->properties['language'] . '.ini';
      		}
      		else
      		{
      			# if everything else fails…
      			$lang_file .= 'German.ini';
      		}
      
      [B]// how to  get $lang_file best?
      [/B]		# read translation table
      		$this->translation = parse_ini_file([U]$lang_file[/U]);
      	}

      Comment

      • Ciary
        Recognized Expert New Member
        • Apr 2009
        • 247

        #4
        srr, can't help you with that. never used language-files

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          I think it'd be best to name the files en.ini, fr.ini, etc. Makes locating the file much easier.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            besides that, is there any other reasonable way (I'd like to preserve the file names for various reasons)?

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              How about naming the files en.English.ini.. that would make parsing easier.

              Something like:

              Code:
              // Assuming you won't have multiple files prefixed with [b]en.[/b]
              // this should return only one file or no files.
              $file = glob("path/to/files/{$lang}.*.inc");
              // $file[0] might hold something like: path/to/files/en.English.inc

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                hmm, that's worth looking into…

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Originally posted by Dormilich
                  hmm, that's worth looking into…
                  It's not ideal, though. It's a shame you can't use * in file_exists().

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    on the other hand, I could auto-generate the config file if the language files have changed…

                    Comment

                    Working...