CodeIgniter Forums
.mo file support in CodeIgniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forum-31.html)
+--- Thread: .mo file support in CodeIgniter (/thread-79328.html)



.mo file support in CodeIgniter - stopz - 05-29-2021

I'm making Composer packages for CodeIgniter and next step is to translate them into few languages using .mo files.
Let me explain why .mo is important:

* Easier standard to work with across companies.

Approach i came up with is:

1. In router allow for CI {locale} definition.
2. In Config/App.php we set defaultLocale to 'en' but fill up supportedLocales with all .mo files that are present.
3. In Language/en/Sample.php whe have something like this:

PHP Code:
<?php

return [
    'GreetingText' => gettext('This text is in English but as it is requested from gettext it should read the .mo file for correct language set by CodeIgniter Request Service.')
]; 

I looked up system/Language/Language.php:134 which is a fallback to English language
-when requested one is not found-

thus this greeting should get triggered from lang('Sample.GreetingText');

Question/Problem: I can't find if CodeIgniter does set up Environment for .mo files and if not or what-not then does anyone get the picture i'm trying to achieve and have any suggestions of what could i try?


RE: .mo file support in CodeIgniter - InsiteFX - 05-29-2021

This would never be put into the framework this is an addon to the application.


RE: .mo file support in CodeIgniter - stopz - 05-29-2021

(05-29-2021, 05:15 AM)InsiteFX Wrote: This would never be put into the framework this is an addon to the application.

Which has nothing to do with what I was looking for Smile Anyone else has any bright ides?


RE: .mo file support in CodeIgniter - stopz - 05-29-2021

Alright i solved my issue. So what helped me alot was https://stackoverflow.com/questions/1949162/how-could-i-parse-gettext-mo-files-in-php4-without-relying-on-setlocale-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!


RE: .mo file support in CodeIgniter - InsiteFX - 05-29-2021

And like I mentioned above an addon Library.