roelofjan-elsinga / aloia-cms

A drop-in flat-file CMS for Laravel.
https://aloiacms.com
MIT License
185 stars 19 forks source link

Translations - Is it possible ? #47

Closed pravnkay closed 1 week ago

pravnkay commented 1 week ago

I want to move from Statamic to Aloia for a very small website that needs translations. Statamic's pro pricing is not justifiable for this website's size.

Aloia seems to a be a perfect fit, if it can support translations. I don't see any hint of that in the documentation. Does Aloia CMS support translations?

roelofjan-elsinga commented 1 week ago

It's all file based, so you can specify a different root folder (collection path) for each language and set this in a service provider in your Laravel app. That's how I'm doing multilingual support at least.

I can provide samples later on today.

pravnkay commented 1 week ago

Please provide a basic example; I hope I can build from there. Meanwhile, I will try to work something out of your idea.

roelofjan-elsinga commented 1 week ago

I'm using this in one of my projects (in a service provider):

public function boot()
{
    $locale = app()->getLocale();

    switch ($locale) {
        case "nl":
        case "fr":
        case "ja":
        case "es":
            config()->set('aloiacms.collections_path', storage_path("app/content/{$locale}_collections"));
            break;
        default:
            config()->set('aloiacms.collections_path', storage_path("app/content/collections"));
    }
}

I combine this with setting the environment variable to the desired locale for the whole app: APP_LOCALE=en. I use separated docker containers for each locale, so I solve this using the environment variable, but you can absolutely do this in middleware as well.

You can also place this code in middleware and set the locale based on the URL or subdomain and set it once at the beginning of the request. I don't have code samples of this approach, but let search engines be your friend on this.

pravnkay commented 1 week ago

Meanwhile, developing from your idea, I have settled on this rather than having multiple folders for each content-type under locale folders.

The content file: lessons.md

---
title: 
  en: "Hello"
  es: "Hola"
description: >-
   Hello World.
list_order: 1
---

A translator helper

<?php

use Illuminate\Support\Arr; 

// Triple Underscore
if (! function_exists('___')) {
    function ___($content = null, $locale = null)
    {
        if (is_null($content)) return $content;
        if(!is_array($content)) return $content;

        $currentLocale = app()->getLocale();    
        $fallbackLocale = app()->getFallbackLocale();

        if(Arr::exists($content, $currentLocale)) {
            return $content[$currentLocale];

        } else if(Arr::exists($content, $fallbackLocale)) {
            return $content[$fallbackLocale];
        }
    }
}

Then in view

  @foreach ($lessons as $lesson)
      {{ ___($lesson->title) }}
      {{ ___($lesson->description) }}
  @endforeach

This way I can keep all the translated content of a particular property inside one file.

Thanks for sharing your approach too. Appreciate it.