Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Lang class with php files
  • Hi,

    I'm new to fuelphp and working on my first project(things going good, it's awesome and fast). 
    My problem is working with the Lang class. My base language is "de". Another languge for example should be "en".
    For example I have this code snippet:

    Lang::load('gui', 'gui_de', 'de');
    Lang::load('gui', 'gui_en', 'en');
    Lang::set('feld', 'wert_de', 'guide', 'de');
    Lang::set('feld', 'wert_en', 'guien', 'en');
    Lang::save('gui', 'gui_de', 'de');
    Lang::save('gui', 'gui_en', 'en');

    this is working fine, i got too files (dir de->gui.php and en->gui.php) with this content.

    <?php
    return array(
    'feld' => 'wert_de',
    );

    and

    <?php
    return array(
    'feld' => 'wert_en',
    );


    when i change the wording, so that i get another line in each file like:

    Lang::load('gui', 'gui_de', 'de');
    Lang::load('gui', 'gui_en', 'en');

    Lang::set('feld2', 'another wert_de', 'gui_de', 'de');
    Lang::set('feld2', 'another wert_en', 'gui_en', 'en');

    Lang::save('gui', 'gui_de', 'de');
    Lang::save('gui', 'gui_en', 'en');

    and run this snippet, the files look like this:

    gui.php de:
    <?php
    return array(
    'feld' => 'wert_de',
    'feld2' => 'another wert_de',
    );

    gui.php en:

    <?php
    return array(
    'feld2' => 'another wert_en',
    );


    where is the Problem ? Sometimes i then have the "de" file mixed with the "en" contetn, why does this one act like it acts ? it drives me crazy -.- 

    Antoher big thing is using the given database table 'lang' fromn the lang docs. How can i use the database ? is there any tutorial ? or any suggestion how i can use the database ? 

    Thanks a lot,
    Ralf

  • To start with your last question: the Lang class uses an "extension" to determine in which format the data is, and where to find it. By default, the ".php" extension is used, which in the above example makes that Lang::load('gui') will load the gui.php file.

    If you want to load from the database, you need to use the ".db" extension, as in Lang::load('gui.db').

    The multiple language saving issue looks like a bug, can you create an issue for it at https://github.com/fuel/core/issues, with a copy of your example, and/or a link to this post?


  • HarroHarro
    Accepted Answer
  • Hi Harro,

    thanks a lot for your fast answer. I will test the Database example and open an issue for the php file problem.

    thanks and regards,
    Ralf
  • wow, that was a fast one :D 
    That's amazing ! 
  • We aim to please... :-)
  • Things now working fine ! 
    Awesome ! 
  • Cool, thanks for the feedback.
  • It looks like there is another bug ... or I'm doing something wrong.
    I get data from my form view and want to save these data. 

    Lang::load('article.db', 'article', 'de');
    Lang::set($article->id, array('name' => Input::post('name_de'), 'description' => Input::post('description_de')), 'article', 'de');
    Lang::save('article.db', 'article', 'de');

    Lang::load('article.db', 'article', 'en');
    Lang::set($article->id, array('name' => Input::post('name_en'), 'description' => Input::post('description_en')), 'article', 'en');
    Lang::save('article.db', 'article', 'en');

    I did 2 Inserts. The first one was easy everything was fine. But after the second one it did thsi

    article de:

    a:2:{i:21;a:2:{s:4:"name";s:5:"Pants";s:11:"description";s:10:"Nice pants";}i:22;a:2:{s:4:"name";s:5:"Kleid";s:11:"description";s:13:"Tolles Kleid.";}}

    article en:
    a:2:{i:21;a:2:{s:4:"name";s:5:"Pants";s:11:"description";s:10:"Nice pants";}i:22;a:2:{s:4:"name";s:5:"Dress";s:11:"description";s:11:"Nice dress.";}}


    instead of writing the 'de' insert to the 'article de' row it wrotes the 'en' part in the 'de' row. The 'en' row worked fine. 

    Do you guys have any idea ? 

    Thanks,
    Ralf

  • oh p.s. I updated the lang core class after the bugfix. 
  • I just ran your code here (slightly modified):

    Lang::load('article.db', 'article', 'de');
    Lang::set('keyname', array('name' => 'GermanName', 'description' => 'GermanDescription'), 'article', 'de');
    Lang::save('article.db', 'article', 'de');

    Lang::load('article.db', 'article', 'en');
    Lang::set('keyname', array('name' => 'EnglishName', 'description' => 'EnglishName'), 'article', 'en');
    Lang::save('article.db', 'article', 'en');

    And this creates:

    INSERT INTO `lang` (`identifier`, `language`, `lang`, `hash`) VALUES
    ('article', 'de', 'a:1:{s:7:"keyname";a:2:{s:4:"name";s:10:"GermanName";s:11:"description";s:17:"GermanDescription";}}', '54116bc8c425b'),
    ('article', 'en', 'a:1:{s:7:"keyname";a:2:{s:4:"name";s:11:"EnglishName";s:11:"description";s:11:"EnglishName";}}', '54116bc8d393a');

    which looks ok?
  • This looks fine and it looks like my first run too.
    But wehen make another insert like: 

    Lang::load('article.db', 'article', 'de');
    Lang::set('keyname', array('name' => 'GermanName2', 'description' => 'GermanDescription2'), 'article', 'de');
    Lang::save('article.db', 'article', 'de');

    Lang::load('article.db', 'article', 'en');
    Lang::set('keyname', array('name' => 'EnglishName2', 'description' => 'EnglishName2'), 'article', 'en');
    Lang::save('article.db', 'article', 'en');

    how is this acting ? 


  • oh and for sure another 'keyname', sorry
  • Did a second insert:

    Lang::load('article.db', 'article', 'de');
    Lang::set('keyname2', array('name' => 'GermanName2', 'description' => 'GermanDescription2'), 'article', 'de');
    Lang::save('article.db', 'article', 'de');

    Lang::load('article.db', 'article', 'en');
    Lang::set('keyname2', array('name' => 'EnglishName2', 'description' => 'EnglishDescription2'), 'article', 'en');
    Lang::save('article.db', 'article', 'en');

    And the result:

    INSERT INTO `lang` (`identifier`, `language`, `lang`, `hash`) VALUES
    ('article', 'de', 'a:2:{s:7:"keyname";a:2:{s:4:"name";s:10:"GermanName";s:11:"description";s:17:"GermanDescription";}s:8:"keyname2";a:2:{s:4:"name";s:11:"GermanName2";s:11:"description";s:18:"GermanDescription2";}}', '54116fed18677'),
    ('article', 'en', 'a:2:{s:7:"keyname";a:2:{s:4:"name";s:11:"EnglishName";s:11:"description";s:11:"EnglishDescription";}s:8:"keyname2";a:2:{s:4:"name";s:12:"EnglishName2";s:11:"description";s:19:"EnglishDescription2";}}', '54116fed30af5');

  • p.s. in both cases the INSERT statement is the result of a PHPMyAdmin export ;-)
  • Hi Harro,

    ok i think this works now for me, another think.
    I've switch to a php file for further testing the Lang::get().


    this is my save:

    Lang::load('article', 'article', 'en');
    Lang::set('article'.$article->id, array(
    'name' => Input::post('name_en'),
    'description' => Input::post('description_en')),
    'article',
    'en'
    );
    Lang::save('article', 'article', 'en');

    this is my php:


    return array(
    'article32' => 
    array(
    'name' => 'Test_2_en',
    'description' => 'Test description',
    ),
    'article33' => 
    array(
    'name' => 'Test_3_en',
    'description' => 'Test 3 description',
    ),
    );
    );

    and this is one example i tried of now about 15 different tries to get my data.

    Lang::load('article', 'article', 'en');
    $helper = Lang::get('article' . $id);

    it always get null.
    Another thing is, where are my groups in the get call ? how can I call my groups ? 

    Thanks for further help,
    Ralf



  • HarroHarro
    Accepted Answer
    This:

    Lang::load('article', 'article', 'en');

    loads the strings into the "article" group, so you need to use

    $helper = Lang::get('article.article' . $id);

    This is what we call "dot-notation". In general, all methods in FuelPHP that access multi-dimensional arrays can be accessed this way, which makes it very easy to retrieve values deep in the array structure.

    In the above situation, you have an internal multi-dimenstional Lang array containing:

    array(
        'article' => array(            // this is the group
            'article32' => array(
                'name' => 'Test_2_en',
                'description' => 'Test description',
            ),
            'article33' => array(
                'name' => 'Test_3_en',
                'description' => 'Test 3 description',
            ),
        );
    );

    In this array,

    Lang::get('article.article33');

    returns

    array(
        'name' => 'Test_3_en',
        'description' => 'Test 3 description',
    ),

    and

    Lang::get('article.article33.name');

    returns

    "Test_3_en"

  • Hi Harro,

    thanks a lot I'm getting more and more into it.

    so another thing is setting the langauge per Lang::load(). It works fine for the set/save workflow. But for the get workflow it seems like it doesn't work. I will will work with Config::set('language' , 'lang'); on each call.

    Thanks a lot,

    Ralf


  • HarroHarro
    Accepted Answer
    Fuel is designed to have only one active language, normally there is no need to have multiple languages active in the application at the same time.

    The only possible exception is when you have a "translation" controller, where you allow the application user to add or modify translations. For which you have to do some additional steps, like you have noticed.

    This is one of the things that will change in Fuel v2, where every language has it's own data container, and multiple can be active at the same time. And since language strings are not loaded into the same array, there will not be any concurrency issue (like you have now, where you are forced to use unique group names for the same lang.php file).

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion