Giter Site home page Giter Site logo

multlang's Introduction

MultLang - PHP Multi-Language Support PHP Composer CI

Overview

  • All strings are saved in XML files
  • Automatic language detection: Methods:
  • Simple string access

Installation

It's very simple to include this library in your project.

Composer

add this to your composer.json and run composer install:

{
"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/StefanSchomacker/MultLang"
        }
    ],
    "require": {
        "multlang/multlang": "dev-master"
    }
}

Download Zip

https://github.com/StefanSchomacker/MultLang/archive/master.zip

Clone Git

git clone https://github.com/StefanSchomacker/MultLang.git

Getting Started

Usage

Just create a config object once and call the load function in your PHP Script.

You can also view the example here.

index.php:

<?php
//get default config or set it manually
$config = Config::getDefaults();
$resource = new Resource($config);
echo $resource->loadString("sample_text");
?>

/dictionary/default.xml:

<?xml version="1.0" encoding="utf-8" ?>
<resources>

    <!-- Header -->
    <string id="sample_text">This is a sample text in english</string>

</resources>

/dictionary/german.xml:

<?xml version="1.0" encoding="utf-8" ?>
<resources>

    <!-- Header -->
    <string id="sample_text">Das ist ein deutscher Beispieltext</string>

</resources>

Configuration

Edit config values if needed:

const default value description
DOCUMENT_ROOT dirname(__DIR__) . DIRECTORY_SEPARATOR Path to document root
PATH_TO_DICTIONARIES DOCUMENT_ROOT . DIRECTORY_SEPARATOR . 'dictionary' . DIRECTORY_SEPARATOR Path to dictionary folder. Folder contains all XML files
DEFAULT_LANGUAGE en Default language in ISO 639-1 format. The constant will be used, if language cannot be determined
DEFAULT_DICTIONARY default.xml This XML file will be used, if other files are not available. This file should contain all string items.
LANGUAGE_DETECTION cookie This defines the method to detect the language. Choose between 'header', 'cookie' and 'rewrite'.
COOKIE_NAME language Default key for the cookie.
SUPPORTED_LANGUAGES array(DEFAULT_LANGUAGE => DEFAULT_DICTIONARY) Contains all supported languages.
Example: "de" => "german.xml",

Example:

<?php
$config = Config::getDefaults();
$config->set(Config::DOCUMENT_ROOT, "/");
?>

Language detection methods

header

This will try to read $_SERVER['HTTP_ACCEPT_LANGUAGE']. If this fails, DEFAULT_LANGUAGE will be returned.

cookie

This will try to read a cookie with the key COOKIE_NAME. If no cookie is set, it will use the header method and set the new cookie.

rewrite

This will try to read $_SERVER['REQUEST_URI'] and split it to identify the language. If no argument is set, it will use the header method
You can use a URL structure like example.com/en/index.php

Details

  • The used language format is ISO 639-1. For more information see https://en.wikipedia.org/wiki/ISO_639-1
  • If the requested string is not available in the XML file, the script will try to search in the DEFAULT_DICTIONARY (default.xml) file. An empty string will be returned if nothing matches.

Improvements

Feel free to create a new Issue or a Pull request

License

Copyright 2020 Stefan Schomacker

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

multlang's People

Contributors

stefanschomacker avatar

Watchers

 avatar

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.