Skip to content

Instantly share code, notes, and snippets.

@romaninsh romaninsh/en.php

Last active Oct 23, 2019
Embed
What would you like to do?
File placed in class namespace atk4/data/locale
<?php
// Automatically generated by lokalise.com for atk4\data (en)
namespace atk4\data\locale;
class En {
const language='en';
const strings=[
'Field requires array for defaults' => 'Field requires array for defaults',
'Field value can not be base64 encoded because it is not of string type' => 'Field value can not be base64 encoded because it is not of string type ({{field}})',
'Mandatory field value cannot be null' => 'Mandatory field value cannot be null ({{field}})',
'Model is already related to another persistence' => 'Model is already related to another persistence',
'Must not be null' => 'Must not be null',
'Test with plural' => [
'zero' => 'Test zero',
'one' => 'Test is one',
'other' => 'Test are {{count}}',
],
'There was error while decoding JSON' => 'There was error while decoding JSON',
'Unable to determine persistence driver from DSN' => 'Unable to determine persistence driver from DSN',
'Unable to serialize field value on load' => 'Unable to serialize field value on load ({{field}})',
'Unable to serialize field value on save' => 'Unable to serialize field value on save ({{field}})',
'Unable to typecast field value on load' => 'Unable to typecast field value on load ({{field}})',
'Unable to typecast field value on save' => 'Unable to typecast field value on save ({{field}})',
];
}
@romaninsh

This comment has been minimized.

Copy link
Owner Author

romaninsh commented Oct 23, 2019

Usage - generic translator:

function _ ($string, $args, $namespace) {
  $language = getCurrentLanguage();
  $class_name = $namespace.'\locale\\'.$language;
  return $class_name::strings[$string];
}

Usage - inside a framework (name-spaced)

namespace \atk4\data\Somewhere;

  $label = _('There was error while decoding JSON', null, 'atk4/data');
  // might use __NAMESPACE__ too, up to author
@romaninsh

This comment has been minimized.

Copy link
Owner Author

romaninsh commented Oct 23, 2019

The benefit of this format is that we do not to EXPLICITLY find and include the file. Composer autoloader will do it for us. Therefore we do not need to manually search and pre-load all the strings. If one of the libraries relies on translations, then class will be loaded on-demand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.