Search code examples
octobercmsoctobercms-plugins

How to view translated county in OctoberCMS rainlab.location plugin


I've rainlab.location and rainlab.translate plugins installed. I translated countries and states in backend, but I can't view them translated in frontend. This is the code I'm using (from documentation):

 {% set countryId = countryId|default(form_value('country_id')) %}
 {% set stateId = stateId|default(form_value('state_id')) %}

 <div class="form-group">
 <label for="accountCountry">Country</label>
 {{ form_select_country('country_id', countryId, {
    id: 'accountCountry',
    class: 'form-control',
    emptyOption: '',
    'data-request': 'onInit',
    'data-request-update': {
        'country-state': '#partialCountryState'
    }
  }) }}
 </div>

 <div class="form-group">
 <label for="accountState">State</label>
 {{ form_select_state('state_id', countryId, stateId, {
    id: 'accountState',
    class: 'form-control',
    emptyOption: ''
 }) }}
 </div>

It displays countries and states in English, even if I switched language!


Solution

  • May be problem is in retrieving records

    https://github.com/rainlab/location-plugin/blob/master/models/Country.php#L71

    when we call directly using ->lists('name', 'id'); translation relation is not loaded.

    but if you change this line from

    return self::$nameList = self::isEnabled()
         ->orderBy('is_pinned', 'desc')->lists('name', 'id');
    

    to

    return self::$nameList = self::isEnabled()
         ->orderBy('is_pinned', 'desc')->get()->lists('name', 'id');
                                    ----- ^ <- this one
    

    It load translation relations and works fine.

    for now you can change locally it should work, in future they might solve it.

    if you are concern about plugin update then you can extract that function and implement your own function.

    Its just form_select_country twig function you can add to your own plugins with different name to make it work as you see there is only small code to copy :)

    https://octobercms.com/docs/plugin/registration#extending-twig

    if any doubt please comment.