Code Monkey home page Code Monkey logo

translator's Introduction

InitPHP Translator

This library; It is a micro library that will allow you to add multi-language support to your projects or libraries.

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Requirements

  • PHP 7.4 or higher

Installation

composer require initphp/translator

Usage

require_once "vendor/autoload.php";
use \InitPHP\Translator\Translator;

$lang = new Translator();
$lang->setDir(__DIR__ . '/languages/')
    ->setDefault('en');

$lang->change('tr'); // Set Current Language

echo $lang->_r('hello');

What does a language file look like?

<?php
return [
    'hello'     => 'Hello {user}',
    'today'     => 'It\'s {day}',
];

File? Directory?

You can use a single file for each language or multiple files under a directory.

Use File

If you are going to use a single file for a language; Your directory structure will look something like this;

/languages/
    en.php
    tr.php
    fr.php

Your code looks like the following;

require_once "vendor/autoload.php";
use \InitPHP\Translator\Translator;

$lang = new Translator;
$lang->useFile(); // Note that it is used first of all.
$lang->setDir(__DIR__ . '/languages/')
    ->setDefault('en');

echo $lang->_r('hello');

Use Directory

If you want to use directories that contain multiple files for each language, your directory structure will be something like this;

/languages/
    en/
        user.php
        admin.php
        profile.php
    tr/
        user.php
        admin.php
        profile.php

Your code looks like the following;

require_once "vendor/autoload.php";
use \InitPHP\Translator\Translator;

$lang = new Translator;
$lang->useDirectory(); // Note that it is used first of all.
$lang->setDir(__DIR__ . '/languages/')
    ->setDefault('en');

// The filename and keyname are separated by dots.
// Example : "filename.key"
echo $lang->_r('user.hello');

Methods

setDir()

Defines the full path to the parent directory where the language files are kept.

Structure :

public function setDir(string $dir): self

setDefault()

Defines the main translation language to be used by default if the desired translation in the current language is not found.

Structure :

public function setDefault(string $default): self

Note : If a text is given by default during use; this library uses the default string you provided at the time of use.

useFile() and useDirectory()

These two methods; tells you whether to use a single php file or a directory for localization. See "File? Directory?" above for more.

Note : If not specified, the file system is used by default.

Structures :

public function useFile(): self;

public function useDirectory(): self;

change()

Changes the current language and loads the desired language if it is not already installed.

Structure :

public function change(string $current): self

_r()

Returns the desired translation value.

Structure :

public function _r(string $key, ?string $default = null, array $context = []): string
  • $key : The key to the desired translation.
  • $default : The string to substitute if the requested translation is not found.
  • $context : An associative array that reports the value of placeholders, if any, in the translation.

_e()

Outputs the desired value directly.

  • $lang->_e('hello') = echo $lang->_r('hello')

Structure :

public function _e(string $key, ?string $default = null, array $context = []): void

Getting Help

If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker.

Contributing

All contributions to this project will be published under the MIT License. By submitting a pull request or filing a bug, issue, or feature request, you are agreeing to comply with this waiver of copyright interest.

  • Fork it ( https://github.com/initphp/translator/fork )
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am "Add some feature")
  • Push to the branch (git push origin my-new-feature)
  • Create a new Pull Request

Credits

License

Copyright © 2022 MIT License

translator's People

Contributors

muhammetsafak avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

deawx evgip

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.