Skip to content

Instantly share code, notes, and snippets.

@romaninsh
Last active October 23, 2019 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romaninsh/4e1fb008826d7855f8daf2f2e022af59 to your computer and use it in GitHub Desktop.
Save romaninsh/4e1fb008826d7855f8daf2f2e022af59 to your computer and use it in GitHub Desktop.
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
Copy link
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
Copy link
Author

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