Welcome Guest, Not a member yet? Register   Sign In
.mo file support in CodeIgniter
#4

(This post was last modified: 05-30-2021, 02:36 AM by stopz.)

Alright i solved my issue. So what helped me alot was https://stackoverflow.com/questions/1949...le-locales this MoParser class.

I have set up a little helper that is linked to service which has a MoParser Factory attached (so to save memory).

Here's the helper:

PHP Code:
if (! function_exists('l'))
{
    /**
    * Returns language from .mo or default CodeIngiter lang.php file.
    * @method l
    * @param  string $index CodeIgniter language_key.
    * @return string Output text.
    */
    function l(string $index):string
    
{
        # Take codeigniter locale.
        $ci_locale service('language')->getLocale();

        # Directory separator & Vendor.
        $DS DIRECTORY_SEPARATOR;
        $VENDOR 'vendor' $DS 'myname';

        # Language containing folder.
        $LANG 'Language';

        # Find .mo file with the name of '$ci_locale'
        # in given directory.

        # Request call body and path from debug backtrace.
        $call_body array_search(__FUNCTION__array_column(debug_backtrace(), 'function'));
        $call_path debug_backtrace()[$call_body]['file'];

        
# Check vendor position in call_path.
        
$vendor_position strpos(strtolower($call_path), $VENDOR);

        # Check if call is going trough vendor path.
        $language_path = ($vendor_position)
            # Check vendor language path.
            substr($call_path0$vendor_position strlen($VENDOR)) . $DS 'src' $DS $LANG

            
# Check application language path.
            $language_path APPPATH $LANG ;

        # Proposed mo file.
        $mo_file $language_path $DS $ci_locale '.mo';

        # Original Codeigniter Language_value.
        $language_value lang($index);

        # Check if .mo file exists.
        if (file_exists($language_path)
        && is_dir($language_path)
        && file_exists($language_path $DS $ci_locale '.mo')
        ) {
            # Bind with language.
            return service('lang')->mo($mo_file$ci_locale)->take($language_value);
        }

        # No .mo? Pass CodeIgniter default.
        return $language_value;
    }


And this is the .mo file factory which is called by the helper to return translations.

PHP Code:
<?php

namespace MyName\MyPackage\Models;

use 
MyName\MyPackage\Libraries\MoParser;

class 
Mo extends MoParser
{
    /**
    * Loaded mo file.
    */
    protected array $mo;

    /**
    * Mo factory construction by file and locale.
    * @method __construct
    * @param  string $mo_file Complete path to file.
    * @param  string $locale language locale.
    */
    public function __construct(public string $mo_file, public string $locale 'en')
    {
        # Load mo file.
        $this->mo $this->loadTranslationData($mo_file$locale);
    }

    /**
    * Retrieves translation from MO or returns original.
    * @method take
    * @param  string $language_value input search value.
    * @return mixed translation or original.
    */
    public function take(string $language_value):mixed
    
{
        # Check locale.
        if (array_key_exists($this->locale$this->mo))
        {
            # Check Mo.
            return (array_key_exists($language_value$this->mo[$this->locale]))
                # Return translation.
                $this->mo[$this->locale][$language_value]

                # Return original.
                $language_value ;
        }
    }


Hope these snippets help anyone stuck with including .mo files!
Reply


Messages In This Thread
.mo file support in CodeIgniter - by stopz - 05-29-2021, 12:40 AM
RE: .mo file support in CodeIgniter - by InsiteFX - 05-29-2021, 05:15 AM
RE: .mo file support in CodeIgniter - by stopz - 05-29-2021, 07:34 AM
RE: .mo file support in CodeIgniter - by stopz - 05-29-2021, 01:40 PM
RE: .mo file support in CodeIgniter - by InsiteFX - 05-29-2021, 08:56 PM



Theme © iAndrew 2016 - Forum software by © MyBB