Jump to:

22989 Posts in 11842 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Multilingual Content - alternative to 'translatable'

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2 3 4 5 6
Go to End
Author Topic: 10609 Views
  • Kalileo
    Avatar
    Community Member
    127 Posts

    Re: Multilingual Content - alternative to 'translatable' Link to this post

    @dab

    if you want the forms, such as shown on the checkout page, multilingual, then, as a first step, you need to enable i18n.

    That alone is not enough though. In the trunk ecommerce module many (but not all) displayed strings are i18n enabled, however even if the translation exists, SS does not find it, and thus not display it (SS 2.3.1).

    Fortunately that is not difficult to fix. Let's take for example the checkout form, personal details. That part of the form is defined in ecommerce/code/model/EcommerceRole.php, and is not i18n enabled yet. The textfields are defined with

    new TextField('FirstName', 'First Name'),

    which needs to get changed to

    new TextField('FirstName', _t('Member.FIRSTNAME','First Name')),

    here is the complete modified function:

       /**
        * Return the member fields to be shown on order forms.
        * For orders made by existing members, this will be called on that member.
        * For new orders, this will be called on the singleton object.
        *
        * Modified by Kalileo 2009-04-15
        * to include " _t('Member.FIRSTNAME','First Name'),"
        * instead of "new TextField('FirstName', 'First Name'),".
        * at the relevant fields in order to enable i18n automatic translations
        *
        * This will use the existing translation in "Member." and "OrderInformation.ss."
        */
       function getEcommerceFields() {
          $fields = new FieldSet(
             new HeaderField(_t('OrderInformation.ss.CUSTOMERDETAILS','Personal Information'), 3),
             new TextField('FirstName', _t('Member.FIRSTNAME','First Name')),
             new TextField('Surname', _t('Member.SURNAME','Surname')),
             new TextField('HomePhone', _t('OrderInformation.ss.PHONE','Phone')),
             new TextField('MobilePhone', _t('OrderInformation.ss.MOBILE','Mobile')),
             new EmailField('Email', _t('OrderInformation.ss.EMAIL','Email')),
             new TextField('Address', _t('OrderInformation.ss.ADDRESS','Address')),
             new TextField('AddressLine2', ''),
             new TextField('City', _t('OrderInformation.ss.CITY','City')),
             new DropdownField('Country', _t('OrderInformation.ss.COUNTRY','Country'), Geoip::getCountryDropDown(), self::findCountry())
          );
          
          $this->owner->extend('augmentEcommerceFields', $fields);
          
          return new CompositeField($fields);
       }

    with that code a call to e.g. domain.tld/de/checkout/?flush=1 should now show the labels of these fields in German (assuming your cart is not empty).

    I hope this helps you to find the required modifications in your code.

  • Cem
    Avatar
    Community Member
    31 Posts

    Re: Multilingual Content - alternative to 'translatable' Link to this post

    Hi,
    I am trying to use the recipe and
    I've already changed the protected function to public function getField($field) { ... as suggested but
    still getting a Fatal Error: Class 'MultiLingual' not found.

    I am on 2.3.1

    Any help is appreciated.

  • Kalileo
    Avatar
    Community Member
    127 Posts

    Re: Multilingual Content - alternative to 'translatable' Link to this post

    To add to my post to posts above, this ticket http://open.silverstripe.com/ticket/3400 explains why often it is (still) necessary to use the long form of the i18n function _t() as in _t('Member.FIRSTNAME','First Name') and not only the short form _t('FIRSTNAME','First Name')

    The short form will apparently work also in nested templates not before 2.3.3. However you can always apply the fix Henk Poley describes in that ticket . Thanks Henk Poley!

  • Apophenian
    Avatar
    Community Member
    46 Posts

    Re: Multilingual Content - alternative to 'translatable' Link to this post

    Cem - Have you created the Multilingual class as outlined in the wiki article, and run a db/build?

  • Cem
    Avatar
    Community Member
    31 Posts

    Re: Multilingual Content - alternative to 'translatable' Link to this post

    Hi Apophenian:
    I did create the Multilingual class and everything as the wiki and eventually gave up trying to implement this workaround.
    My problem however is solved with the latest beta release 2.3.2 and translatable seems to be working again.
    Thanks for your response anyways!
    Cheers
    Cem

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: Multilingual Content - alternative to 'translatable' Link to this post

    Hey with help from simon_w I got the homepage working as expected in 2.3.2, I changed the code to:

    class MultiLingual extends ModelAsController {   
       static $lang;   
       function init() {
          $baseUrl = Director::baseUrl();
          $requestUri = $_SERVER['REQUEST_URI'];
          $lang = substr($requestUri, strlen($baseUrl), 2);      
          self::$lang = $lang;
          //prevents default behaviour of redirecting to '/' for '/en/'
          if($this->URLParams['URLSegment'] == "" || $this->URLParams['URLSegment'] == 'home') {
             $urlparams = array('URLSegment' => 'home', 'Action' => ' ');
             if(self::$lang == 'mr'){
                $_POST['this_is_a_hack_to_stop_the_home_redirect'] = true;
             }
             $this->setURLParams($urlparams);
          }      
          parent::init();
       }
       static function currentLang() {
          return self::$lang;
       }   
    }

    The if(self::$lang == 'mr') isn't totally required I don't think, but have a play and reply if this works for you.

  • CriaturaCreativaStudio
    Avatar
    Community Member
    73 Posts

    Re: Multilingual Content - alternative to 'translatable' Link to this post

    Hello!

    Is there a way to display custom includes evaluating the current language?
    I find this very difficult to do, and since i'm not a programmer, all that i've tried was messing up the layout or giving me errors...

    All i need is to have a conditional that evaluates current language, to display a custom sidebar, wich now is an include from Page.ss.

    If anyone can post here a way to evaluate the language inside the .ss files to include another, that would be great !

    Thanks in advance,

    Regards

    Eduardo

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: Multilingual Content - alternative to 'translatable' Link to this post

    Yup, this is how i do it. I include the following function in my Page class:

    public function LangIsMaori() {
          $lang = MultiLingual::currentLang();
          if($lang == 'mr'){
          return true;
          }
       }


    Which then allows me to use this anywhere in my templates:

    <% if LangIsMaori %>
    <h2>Ngā mahi whai muri mai</h2>
    <% else %>
    <h2>Upcoming Events</h2>
    <% end_if %>

    10609 Views
Page: 1 2 3 4 5 6
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.