PHP code example of crosa7 / leaf-omniglot

1. Go to this page and download the library: Download crosa7/leaf-omniglot library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

crosa7 / leaf-omniglot example snippets


// Create a folder named "locales" in your project root
// (you can also create in another place that makes more sense, just be aware to configure the path accordingly)

// Create the following file inside the "locales" folder
en_US.locale.json

// With the following content
{
    "welcome.title": "Hello World"
}

omniglot()->init(
    [
        'TRANSLATION_FILES_LOCATION' => './locales',
    ]
);

omniglot()->addLanguageSwitchRoute();

<form method="post" action="/language/switch">
  <select name="locale" onchange="this.form.submit()">
    @foreach(omniglot()->getAvailableLocales() as $locale)
        <option value="{{ $locale }}" {{ omniglot()->getCurrentLocale() === $locale ? 'selected' : '' }}>{{ $locale }}</option>
    @endforeach
  </select>
</form>

<h1>{{ tl('welcome.title') }}</h1>

<form method="post" action="/language/switch">
    <select name="locale" onchange="this.form.submit()">
      
	foreach(omniglot()->getAvailableLocales() as $locale) {
            if (omniglot()->getCurrentLocale() === $locale) {
                echo '<option value=' . $locale .' selected>' . $locale . '</option>';
            } else {
                echo '<option value=' . $locale .'>' . $locale . '</option>';
            }
	}
      

// Create the following file inside the "locales" folder
pt_PT.locale.json

// With the following content
{
    "welcome.title": "Ola Mundo"
}

{
    "welcome.page.title": "This is the page title translation",
    "welcome.page.sub_title": "This is welcome page subtitle"
}

omniglot()->init([
    'DEFAULT_LOCALE' => 'en_US',
    'TRANSLATION_FILES_LOCATION' =>  './locales',
]);

DEFAULT_LOCALE => en_US

// Translation file name
en_US.locale.json

// Your class: RequestLocaleStrategy
CUSTOM_LOCALE_STRATEGY_CLASS_NAME => RequestLocaleStrategy::class

// Translation file content
{
    "welcome.page.title": "Welcome %firstName% --lastName-- to the dashboard",
    "navbar.title": "Dashboard"
}

// Translation method call
tl('welcome.page.title', ['%firstName%' => 'John', '--lastName--' => 'Doe']); // Welcome John Doe to the dashboard

// You can use anything as parameter identifier. Omniglot will look for anything that you pass as key  
// in the parameters array and replace anything that it finds with this pattern and replace by the value you pass.

// In case of translations without parameters you can simply call:
tl('navbar.title'); // Dashboard


{
    "locale": "en_US"
}

en_US.locale.json
pt_PT.locale.json