See punic.github.io for more info.
We are always happy to get feedback or even pull requests. In order to keep the code consistent, please use the wonderful php-cs-fixer.
PHP translation and localization made easy!
Home Page: https://punic.readthedocs.io
License: Other
See punic.github.io for more info.
We are always happy to get feedback or even pull requests. In order to keep the code consistent, please use the wonderful php-cs-fixer.
Many websites / online stores which have fields for address only show the required information for that particular country for filling in the address. In particular, this mostly concerns the state/provice/administrative area field which is not required by default in most countries.
I would suggest to add information in Punic for it to be able to tell the required fields for the country's address. This information can be fetched from Google's address data:
https://chromium-i18n.appspot.com/ssl-address
Definitions for the Google's data:
https://github.com/google/libaddressinput/wiki/AddressValidationMetadata
In the Google's data this can be found inside the require
key for each country.
In addition, I think it would be great if Punic included the address formatting data, i.e. how to format the address string in each country. This can be also different depending on the country. That is also included in the Google's data inside the fmt
key.
There is another project that already provides that but due to its external dependencies it may be difficult to install on some existing projects (you know which one):
https://github.com/commerceguys/addressing
I believe this would fit Punic's scope quite good as well.
There's no public licensing information for the data but the Java library itself is Apache License 2.0 and the specification page says "open-source address metadata". Here's also some discussion regarding the license (no official answer from Google but at least it can be expected to be under Apache License 2.0):
google/libaddressinput#155
Using package punic/punic:3.6.0
to describe an interval of one year with a width of long
and the locale de
leads to an incorrect result, which is immediately apparent to any German native speaker.
The following code prints the long description of an interval of one year in German:
$start = new \DateTimeImmutable();
$end = $start->add(new \DateInterval('P1Y'));
echo \Punic\Calendar::describeInterval($end, $start, 1, 'long', 'de'), PHP_EOL;
The code above should print the following correct output:
1 Jahr
The code above instead prints the following incorrect output:
1 Jahrs
This package sources its locale data from https://github.com/punic/data, which in turn is sourced from https://github.com/unicode-org/cldr.
Executing the following command in punic/data's repository shows that it already contains the incorrect locale data:
$ php -r "echo (require 'docs/2/39/de/units.php')['long']['duration']['year']['one'], PHP_EOL;"
%1$s Jahrs
Inspecting unicode-org/cldr's repository shows that it contains the correct data: https://github.com/unicode-org/cldr/blob/651afecf9ccf1541a49306993e8210fa2209aa0b/common/main/de.xml#L8377 Therefore the error must be in punic/data's CLDR conversion. We can also see that unicode-org/cldr contains data for different grammatical cases (see also the table at https://de.wiktionary.org/wiki/Jahr). I would surmise that punic/data does not handle the grammatical cases correctly. It probably combines all grammatical cases, which makes the last one listed in the file win, which would be https://github.com/unicode-org/cldr/blob/651afecf9ccf1541a49306993e8210fa2209aa0b/common/main/de.xml#L8380, which is what is present in punic/data.
I wonder whether we should hide those two "countries" by default. Do people from those places want to select their place as a country? Would they pick UK? Isle of man doesn't belong to the UK.. Anyone who knows more about this than I do?
concrete5 removes those two entries:
http://concrete5.org/api/source-class-Concrete.Core.Localization.Service.CountryList.html#_loadCountries
Follow up of #158 (comment)
I'd prefer copying the telephone data from CLDR 33 into future punic-data releases, rather than patching data from future CLDR releases back into to CLDR 33.
@c960657 I'd prefer to have an up-to-date reference data.
We simply need a list of territory code -> phone prefixes. Are you aware of such list that's reliable and up-to-date?
At the end of October, version 44 of CLDR was released. Are there plans to add support for it?
It should be replace StatesProvincesList in concrete5
$script, $territory and $language are not defined
Would it be possible include measurement data as well (found in /supplementalData/measurementData)?
It would be great as I am building a library for each country and all their settings.
@bantu FYI – this library is used by ownCloud as well, the test suite is failing on HHVM with:
There was 1 failure:
1) CalendarTest::testFormat
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-GMT-1:02
+Fiji Standard Time
Thought not sure how relevant a test case containing Fiji Standard Time for us is…
Does it support to generate a list of timezone in the world?
@c960657 I'd accept a pull request that adds your details to the authors
section of composer.json
😉
Complete http://punic.github.io with some general hints
It seems the nl
locale currently returns an empty array instead of an empty string when any getTimezoneNameNoLocationSpecific has no value.
$tz = new \DateTimeZone('Asia/Kathmandu');
$locale = 'nl';
var_dump(\Punic\Calendar::getTimezoneNameNoLocationSpecific($tz, 'short', '', $locale));
I have seen no other locale behave this way. And it doesn't adhere to the method signature.
src/data/en/localeDisplayNames.php
includes useful translations for things such as Sort Orders, Calendar Names, etc. How can these be accessed?
So far I never cared much about performance as it was never a requirement for me, but there's definitely quite some potential.
Hey,
I love what you did with this library, however it is missing a currency formatter that takes in the amount and the currency and prints it out nicely,
This feature would be pretty useful, I tried looking everywhere in the API docs and couldn't find any but alot of the Node libraries that do the same thing (from CLDR) have a function like that.
Having issues when comparing special characters...
// file ./concrete/vendor/punic/punic/code/Comparer.php
// .....
public function compare($a, $b)
// .....
// failing when comparing "Latvia" and "St. Barthélemy"
$result = $this->caseSensitive ? strnatcmp($a, $b) : strnatcasecmp($a, $b)
// .....
Each piece of CLDR data comes with a draft status. See: http://cldr.unicode.org/index/process
approved (default if not specified)
contributed
provisional
unconfirmed
Punic is currently reading all four.
IMHO, provisional and unconfirmed data should be ignored by default. Possibly, an additional parameter in Punic API could be added, to explicitly allow use of provisional or unconfirmed data.
This is a question.
Is there a way, out of the box, to use a DI/IoC type approach with this library. I see most of the singleton methods take a locale:
Calendar::getMonthName($dt, 'wide', 'it');
And if you want to use these functions everywhere in the application without constantly setting the locale, you can set the locale with the Data
singleton:
Data::setDefaultLocale();
Calendar::getMonthName($dt, 'wide');
Does this mean that the locale is globally set everywhere? so you couldn't for example inject an environment to one set of classes with one default locale and another environment to another? For example something like:
$firstPunic = new Punic();
$firstPunic->setDefaultLocale('en');
$secondPunic = new Punic();
$secondPunic->setDefaultLocale('it');
$usesFirst = new UsesFirstEnvironment($firstPunic);
$usesSecond = new UsesSecondEnvironment($secondPunic);
final class UsesFirstEnvironment
{
private $punic;
public function __construct(Punic $punic)
{
$this->punic;
}
public function aMethod()
{
$this->punic->calendar()->getMonthName($dt, 'wide');
}
}
final class UsesSecondEnvironment
{
private $punic;
public function __construct(Punic $punic)
{
$this->punic;
}
public function aMethod()
{
$this->punic->calendar()->getMonthName($dt, 'wide');
}
}
If something a long these lines is not possible, I may have a PR implementing something like this at some stage (because my application can't work if I have to set globals on a singleton). Would you be interested in that PR?
Punic\Unit::format formats a number associated to a unit.
For instance, with \Punic\Unit::format(2, 'millisecond', 'long', 'en')
we have 2 milliseconds
.
But we should also format the number.
Currently we have that \Punic\Unit::format(2000, 'millisecond', 'long', 'en')
returns 2000 milliseconds
, whereas we should obtain 2,000 milliseconds
.
Problem: how to pass the wanted number of decimals (ie the $precision
argument) to \Punic\Number::format()
?
What about integrating it in the $width
parameter of \Punic\Unit::format
?
So, for instance:
\Punic\Unit::format(2000, 'millisecond', 'long', 'en')
=> 2,000 milliseconds
\Punic\Unit::format(2000, 'millisecond', 'long,2', 'en')
=> 2,000.00 milliseconds
In the auto-generated files, subdivisionContainment.php
uses the deprecated code for China subdivisions such as chah
while the data file uses the new code ch34
. This happens for all subdivision that has a new numeric code.
This issue raise the following error when using Territory::getList('s')
:
Undefined index: cnah
I've just run into an issue with the localePattern for Korean. On closer inspection I notice this is not consistent with other languages, 'punic/code/data/ko/localeDisplayNames.json'
localePattern":"%1$s(%2$s)
All other languages use
localePattern":"%1$s (%2$s)
Currently I retrieve the following myself parsing the /common/supplemental/supplementalData.xml file, however would appreciate if you could include these into Punic:
From /supplementalData/weekData:
From /supplementalData/codeMappings/territoryCodes:
/supplementalData/territoryContainment/group, as I need these to identify if a country is part of the EU community.
Thanks in advance!
Hi,
Thanks for the great library! Just a quick question: any reason why you have implemented some number and date formatting functions whereas PHP's intl extension has built in classes (NumberFormatter/IntlDateFormatter), which are based on the same ICU/CLDR components?
Thanks! Sacha
Expected: Can build more language data.
Actual: Build script missing, can't build more language data.
punic-3.1.0/bin/build.sh build script is missing, and so "To keep the size of the package at a reasonable level, only data for a limited set of languages (about 40, defined here) is included by default. If you need more, you can build them yourself using the bundled build script." is incorrect in the documentation, as no actual build script is provided.
I just created https://github.com/punic/data which contains data files with all the CLDR languages, in PHP Punic format.
We now need to get rid of the punic-build-data
command in the bin
folder, and add a new command that fetches these data files.
Specifically I would like to see support for nn
. It would also be nice if the readme could mention or link to a list of supported languages, and describe the process for adding more. Let me know if I can help with something.
Hey,
I know I can build with more locales, but it is a pain to maintain a fork for an open source project just to have one or two more locales in the default list.
May I suggest that you add at least the top 20 countries with most internet users?
http://www.internetworldstats.com/top20.htm
This makes more sense. Currently, you have 40 locales in the package and to have these 20 countries on the list you need to add less than 5 more locales.
I also suggest that you check out the top 10 languages used on the web:
http://www.internetworldstats.com/stats7.htm
For example, you are clearly missing the Malaysia.
Ultimately, PHP is a scripting language used mostly used in web, so there should be a logical reason for choosing the locales to bundle as default with this library.
All being said, I used your library in the last couple of days and I should say, well done and thanks for developing such a life-saving piece of code. This is actually the best library I could find that do not download or cache CLDR information while running and don't need curl, allow_url_fopen or similar functions.
When I hand over a DateTimeImmutable object to Punic, it throws an exception.
Please use the DateTimeInterface when checking the type instead of DateTime.
Hello, thank you for the amazing library! There's a new CLDR dataset at http://cldr.unicode.org/index/downloads/cldr-37 and another one coming out soon.
If there is a way to update the data manually myself, please let me know. Thank you! :)
Hi
Thanks very much for creating this. I am using in a current project and am trying to format my dates:
Calendar::describeInterval($time, $now, 1, 'long');
This is working and shows something like this: 4 months
I wanted this to be able to show if it is in the past or future: 4 months ago in 3 days etc.
I can see in the data that this looks ready to use but can't find in the documentation on how to format like this? Any advice?
Thanks,
Punic ships with v27.
Calendar::convertPhpToIsoFormat has some problems (I trusted too much ZF1).
It would be great if Punic could correctly parse and use the "en-US-u-va-posix" locale (or the equivalent "old style" en_US_POSIX). Currently \Punic\Exception\InvalidLocale is thrown if that locale is used.
Hello,
I have encountered an odd issue which I can't fully explain.
echo \Punic\Language::getName('ar','fi');
I got:
Notice: Trying to access array offset on value of type null in /home/customer/www/common/composer/vendor/punic/punic/src/Data.php on line 692
Fatal error: Uncaught Punic\Exception\InvalidLocale: A valid locale should be a string, NULL received in /home/customer/www/common/composer/vendor/punic/punic/src/Data.php:682
Stack trace:
#0 /home/customer/www/common/composer/vendor/punic/punic/src/Data.php(719): Punic\Data::getLocaleAlternatives(NULL, false)
#1 /home/customer/www/common/composer/vendor/punic/punic/src/Data.php(654): Punic\Data::getLocaleAlternatives('fi')
#2 /home/customer/www/common/composer/vendor/punic/punic/src/Data.php(261): Punic\Data::getLocaleFolder('fi')
#3 /home/customer/www/common/composer/vendor/punic/punic/src/Language.php(75): Punic\Data::get('languages', 'fi')
#4 /home/customer/www/common/web/classes/Common/LanguageStrings.php(992): Punic\Language::getName('ar', 'fi')
#5 /home/customer/www/common/web/classes/Common/LanguageStrings.php(899): Common\LanguageStrings->updateLanguageStringsLanguage()
#6 /home/customer/www/dev.webcal.guru/classes/WebCalGuru/Administration.php(2312): Common\LanguageStrings->updateLanguageStrings()
#7 /home/custo in /home/customer/www/common/composer/vendor/punic/punic/src/Data.php on line 682
The issue seemed to be in Data.php, row 536
$chunks = explode('-', str_replace('_', '-', strtolower($locale)));
The output of strtolower($locale) is fi-latn-fI
(notice capital I in the end). I tested with every way I could, the strtolower('I') always gave the same upper case I. I've tried everything possible, and it simply does not give lower case i.
When I changed strtolower to mb_strtolower, it started working again.
I cannot explain this. Is it just me, or is there some UTF8 sequence involved? Perhaps in a data file, under Finland territory, the ending I is some UTF8 character, which mb_strtolower recognises as I, but strtolower does not?
Would it be possible to get the Postalcode data from postalCodeData.xml? I am interested also getting the regular expressions of the postcodes.
Thanks! Sacha
Hi,
It would be great if also the supplemental data could be made available. In particular I am interested in:
Thanks! Sacha
We currently support nn
as the only Norwegian language:
https://github.com/punic/punic/blob/master/bin/build.php#L122
I have some doubts about that, because from what I can see Bokmål is spoken by more people:
A 2005 poll indicates that 86.3% use primarily Bokmål as their daily written language, 5.5% use both Bokmål and Nynorsk, and 7.5% use primarily Nynorsk.
https://en.wikipedia.org/wiki/Norwegian_language
I don't know much about the languages there, but it seems to be that we should add nb
or replace nn
with nb
. Is there anyone who knows more about that?
If I try to create a new Comparer with a locale that is not 'en' and I do not have the 'intl' PHP extension installed but I do have the Symfony\Component\Intl\Collator\Collator loaded, I get an exception thrown when it could be handled gracefully.
An edge case that can be corrected externally, closing the issue, sorry!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.