Translatable fields in new module - bug or expected behaviour



  • I'm developing a module that has a mix of translatable and non translatable fields.

    in my partials/create-fields.blade.php I have something like this:

     {!! Form::i18nInput('salutation', trans('module::section.form.salutation'), $errors, $lang) !!}
    
    {!! Form::normalInput('first_name', trans('module::section.form.first_name'), $errors) !!}
    
    {!! Form::normalInput('last_name', trans('module::section.form.last_name'), $errors) !!}
    
    {!! Form::i18nInput('country', trans('module::section.form.country'), $errors, $lang) !!}
    

    Let's say i have 2 languages to support 'en' and 'fr' - so there will be an English and French tab.

    The problem is that fields that don't need translation appear on both tabs - meaning that the user has to duplicate input unnecessarily .

    If the user completes all the fields on the English tab, then they should only need to input the translatable values on the French or other language tabs. However if they leave the non-translatable fields blank on the french tab then the underlying entity columns are not populated with the English values. The translated_tabvle gets populated ok.

    e.g.

    fieldname: en, fr
    salutation: Mister, Monsieur
    first_name: John,
    last_name: Doe
    country: United States, Etats Unis

    The translation table will have the correct entries for the translatable fields but the main table will have empty (non null) values for first_name and last_name.

    It's not really practical to separate translatable and non-translatable fields (in a similar way to Custom fields for the Page module) in this module due to the way the data is grouped. Also I don't want user's to have to repeat the data entry across tabs (there are about 35 fields for them to enter, about 50% are non-translatable) as additional languages will be added as the app is rolled out across regions.

    So is this expected behaviour or a bug. If a field is non-translatable (normalInput) should it not use the values in the first language tab or the sites default language?



  • Hi,

    You need to put the non-translatable field outside the tab-pane and outside the loop of the language

    https://github.com/AsgardCms/Page/blob/master/Resources/views/admin/edit.blade.php#L31

    If you put it before, it will be present on every tabs and therefore editable, but the input will be present only once, if it's in the loop it will be repeated.

    This one will work well:

    <div class="tab-content">
    {!! Form::normalInput('first_name', trans('module::section.form.first_name'), $errors) !!}
    <?php $i = 0; ?>
    <?php foreach (LaravelLocalization::getSupportedLocales() as $locale => $language): ?>
    <?php ++$i; ?>
    <div class="tab-pane {{ App::getLocale() == $locale ? 'active' : '' }}" id="tab_{{ $i }}">
    @include('page::admin.partials.edit-fields', ['lang' => $locale])
    </div>

    Btw, where can I find the doc about how to display the code in a decent format ?



  • @motchju I understand that but it's not practical in my scenario for UI/UX reasons and the way the data is grouped to separate the translatable and non-translatable fields.

    My quick solution is as follows:

    I have 2 partials for the create and edit fields:

    partials/create-fields.blade.php
    partials/create-fields-translatable.blade.php
    partials/edit-fields.blade.php
    partials/edit-fields-translatable.blade.php

    the create-fields.blade.php and edit-fields.blade.php have all the fields as normal.

    the create-fields-translatable.blade.php looks like this:

     {!! Form::i18nInput('salutation', trans('module::section.form.salutation'), $errors, $lang) !!}
    
    {!! Form::normalInput('first_name', trans('module::section.form.first_name'), $errors, null, ['disabled'=>'disabled', 'placeholder'=>trans('modules::section.placeholders.non_translatable')]) !!}
    
     {!! Form::normalInput('last_name', trans('module::section.form.last_name'), $errors, null, ['disabled'=>'disabled', 'placeholder'=>trans('modules::section.placeholders.non_translatable']) !!}
    
    {!! Form::i18nInput('country', trans('module::section.form.country'), $errors, $lang) !!}
    

    and the edit-fields-translatable.blade.php

     {!! Form::i18nInput('salutation', trans('module::section.form.salutation'), $errors, $lang) !!}
    
    {!! Form::normalInput('first_name', trans('module::section.form.first_name'), $errors, null, ['disabled'=>'disabled']) !!}
    
     {!! Form::normalInput('last_name', trans('module::section.form.last_name'), $errors, null, ['disabled'=>'disabled']) !!}
    
    {!! Form::i18nInput('country', trans('module::section.form.country'), $errors, $lang) !!}
    

    where the placeholder text reads something like 'This field is non-translatable, please enter the value on the site's default language tab'

    then in the create.blade.php and edit.blade.php files I have the following:

    @foreach (LaravelLocalization::getSupportedLocales() as $locale => $language)
       <?php $i++; ?>
           <div class="tab-pane {{ locale() == $locale ? 'active' : '' }}" id="tab_{{ $i }}">
           @if( config('app.fallback_locale') == $locale )
               @include('module::admin.section.partials.create-fields', ['lang' => $locale])
           @else
              @include('module::admin.section.partials.create-fields-translatable', ['lang' => $locale])
           @endif
           </div>
     @endforeach
    

    Whilst it's not particularly elegant it does what I need it to do - the user doesn't have to fill in the same field multiple times but they can see the data structure and are told where to fill the data in for the create.

    When I have a bit more time I'll amend it so that none of the fields are disabled and add an onBlur event to the non-translatable fields so that when they complete a field on one tab it will auto-populate all the other tab values.


  • Global Moderator

    @motchju Forum uses MarkDown to style things


Log in to reply