<?php

namespace Plainwork\Core;

class Translate
{
    private $ret = [];

    public function translateString($in, $lang = 'en')
    {
        return $in;
    }

    public function translate($ret, $lang = 'en')
    {
        if (isset($this->ret[$lang][$ret])) {
            return $this->ret[$lang][$ret];
        }

        $parts = [];

        $start = '__';
        $startLen = strlen($start);
        $end = '__';
        $endLen = strlen($end);

    // find strings to translate
        $replace = [];
        $pos1 = strpos($ret, $start);
        while (false !== $pos1) {
            $pos2 = strpos($ret, $end, $pos1 + $startLen + 1);
            if (false === $pos2) {
                break;
            }

            $pos2 = $pos2 + $endLen;
            $part = substr($ret, $pos1, $pos2 - $pos1);
            $toString = substr($part, $startLen, -$endLen);

            $replace[$part] = $toString;

            if (($pos2 + 1) > strlen($ret)) {
                break;
            }

            $pos1 = strpos($ret, $start, $pos2 + 1);
        }

    // do translate
        if ('en' !== $lang) {
            foreach (array_keys($replace) as $k) {
                $replace[$k] = $this->translateString($replace[$k], $lang);
            }
        }

    // replace in output
        $ret = strtr($ret, $replace);
        // $ret = str_replace(array_keys($replace), array_values($replace), $ret);
        $this->ret[$lang][$ret] = $ret;

        return $ret;
    }
}
