4

I'm trying to write some PHP code to return a translation based on two parameters: an integer translation id and a two-letters language code.

For this, I nested switch statements. The outer switch works fine, but the inner switch fails and their default is always returned, whichever the value that the $lang parameter takes. In the example below, "Berne" is returned when $lang="de".

I checked, that the value of $lang is correct; it was. Casted $lang as (string) to be sure that it is of the correct type. Tried enclosing the inner switches between brackets, as well as putting double quotes instead of simple ones. Without success. Here's my code:

<?php
switch($id) {
  case 3:
    {
    switch((string)$lang) {
      case 'de':
        $v = 'Bern';
        break;
      case 'en':
        $v = 'Berne';
        break;
      default:
        $v = 'Berne';
      }
    }
    break;

  case 4:
    {
    switch($lang) {
      case 'de':
        $v = 'Zürich';
        break;
      case 'en':
        $v = 'Zurich';
        break;
      default:
        $v = 'Zurich';
      }
    } 
    break;

  default:
    {
    switch($lang) {
      case 'de':
        $v = 'Genf';
        break;
      case 'en':
        $v = 'Geneva';
        break;
      default:
        $v = 'Genève';
      }
    }
    break;
}
return $v;
?>
3
  • Put var_dump($lang);var_dump($lang=='de'); after both $v = 'Berne'; and check again. Commented Aug 30, 2015 at 16:06
  • You might as well echo mb_detect_encoding($lang) to make sure it is in the correct encoding and you might want to trim($lang) too.
    – frz3993
    Commented Aug 30, 2015 at 16:39
  • Thanks. The issue was not in the code itself, but in the fact it was used as a snippet of MODx content management system, which has a server side caching mechanism. Hence, the code was returning the same values as from the first call. I made the snippet uncached and this solved the problem.
    – OuzoPower
    Commented Aug 31, 2015 at 15:35

1 Answer 1

2
//I don't what error you are getting when i am testing your code its working perfectly you can also see
<?php
$id = 4;
$lang="de";
switch($id) {
  case 3:
    {
    switch((string)$lang) {
      case 'de':
        $v = 'Bern';
        break;
      case 'en':
        $v = 'Berne';
        break;
      default:
        $v = 'Berne';
      }
    }
    break;

  case 4:
    {
    switch($lang) {
      case 'de':
        $v = 'Zurich1';
        break;
      case 'en':
        $v = 'Zurich2';
        break;
      default:
        $v = 'Zurich3';
      }
    } 
    break;

  default:
    {
    switch($lang) {
      case 'de':
        $v = 'Genf';
        break;
      case 'en':
        $v = 'Geneva';
        break;
      default:
        $v = 'Genève';
      }
    }
    break;
}
echo $v;
?>
1
  • Thanks. I realized the code was working perfectly. The problem came from the fact that I was using it as a snippet of the MODx Content Management System, which has a server-side caching mechanism.This snippet was itself called from a chunk (=sub-template) used by another snippet. I inverted the cache/non-cache status of both snippets and this solved the problem.
    – OuzoPower
    Commented Aug 31, 2015 at 15:33

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