Skip to content

grogorick/minimal-php-localization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 

Repository files navigation

MINIMAL PHP LOCALIZATION

Localization file format (yaml-like)

# COMMENT
LABEL:
 LOCALE-1: "Text for locale 1"
 LOCALE-2: "Text for locale 2"


# Examples
hello:
 en: "Hello!"
 de-DE: "Hallo!"
 de-AT: "Griaß di!"

points:
 en: "%s Points"
 de: "%s Punkte"
progress:
 en: "Step %s of %s"
 de: "Schritt %s von %s"

help:
 en: "This is a rather long help text
      that spans multiple lines."
 de: "Das ist ein längerer Hilfetext
      der über mehrere Zeilen geht."

Initialization

PHP

<?php
require('localization/localization.php');
LOCALIZATION\INIT_FROM_FILE('localization.yaml');
LOCALIZATION\SET_LOCALE(@$_GET['lang']);
use function LOCALIZATION\L;
?>
  • load localization dictionary from file
  • set locale from URL parameter, or try to get it from browser (header), or use default (en-US) otherwise
  • make localization function L() available in PHP

JS

<?=LOCALIZATION\INIT_JS()?>
  • generate a <script> tag to make localization function L() available in JS

Usage

PHP

<?=L('LABEL')?>

JS

<script>L('LABEL')</script>

Examples

  • L('hello') — Get localized string
  • L('points', 42) — Get localized string, with a contained %s replaced by the supplied argument
  • L('progress', 99, 100) — Any number of arguments can be supplied, e.g., two

Locale matching

Given a requested label and the current locale, e.g., de-DE, the localized entries for that label in the dictionary are (in that order) searched for locales that

  • exactly match de-DE
  • start with de-DE
  • exactly match de
  • start with de

The first entry is selected if no locale matched.
The label itself is returned if it's not contained in the dictionary.

About

Zero dependency PHP localization/internationalization from a YAML file

Topics

Resources

License

Stars

Watchers

Forks

Languages