Creating a language programmatically looks straightforward and is not. This fails, silently at first and then confusingly:
$lang = new \IPS\Lang; $lang->short = 'he'; // does nothing useful $lang->title = 'עברית'; $lang->save(); // lang_short is left EMPTY
set_short() is a side effect, not a setter
Lang::set_short() calls setlocale(), stores the resulting locale data and, on Windows, the codepage. It never assigns lang_short. So the column stays empty, the getter returns NULL, and then languageInit() — which calls set_short( $this->short ) — throws a TypeError the moment anything touches the object.
The AdminCP avoids all of this because lang_short arrives through form values rather than through the property.
What actually works
$id = \IPS\Db::i()->insert( 'core_sys_lang', array(
'lang_short' => 'he_IL',
'lang_title' => 'עברית',
'lang_isrtl' => 1,
'lang_enabled' => 1,
) );
// constructFromData, not load() - load() returns whatever is in the
// multiton cache, which for a row created this instant is nothing useful
$lang = \IPS\Lang::constructFromData(
\IPS\Db::i()->select( '*', 'core_sys_lang', array( 'lang_id=?', $id ) )->first()
);
Why an empty locale is worse than it looks
- Right-to-left cannot be detected. Nothing can tell that a language is Arabic if there is no code to read, so an RTL community renders left-to-right and looks broken.
- The flag in the language chooser is derived from the locale, falling back to treating a bare language code as a country code. So
arshows the flag of Argentina next to العربية, andzh_Hansshows no flag at all because it is not a country. Always include a region:ar_SA,zh_CN,he_IL.
A new language has no strings at all
Creating a language does not copy the string table. It has zero rows in core_sys_lang_words and the suite falls back to the default language for anything missing. So translating a language means inserting rows, and "how complete is it" is a comparison against the default language rather than a count of empty fields.
That is also why a half-finished language is a working site rather than a broken one.
Recommended Comments